Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add optional keybindings #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 deletions codejar.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
const globalWindow = window

type JarFunctions = {
updateCode: (code: string) => void;
save: () => Position;
restore: (pos: Position) => void;
};

type Keybinding = {
key: string;
ctrl?: boolean;
meta?: boolean;
shift?: boolean;
alt?: boolean;
action: (editor: HTMLElement, jar: JarFunctions) => void;
};

type Options = {
tab: string
indentOn: RegExp
moveToNewLine: RegExp
spellcheck: boolean
catchTab: boolean
preserveIdent: boolean
addClosing: boolean
history: boolean
window: typeof window
tab: string;
indentOn: RegExp;
moveToNewLine: RegExp;
spellcheck: boolean;
catchTab: boolean;
preserveIdent: boolean;
addClosing: boolean;
history: boolean;
window: typeof window;
keybindings?: Keybinding[];
}

type HistoryRecord = {
Expand Down Expand Up @@ -85,6 +101,23 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
}
}, 300)

const handleKeybindings = (event: KeyboardEvent) => {
if (options.keybindings) {
for (const binding of options.keybindings) {
if (
event.key.toLowerCase() === binding.key.toLowerCase() &&
!!binding.ctrl === event.ctrlKey &&
!!binding.meta === event.metaKey &&
!!binding.shift === event.shiftKey &&
!!binding.alt === event.altKey
) {
event.preventDefault();
binding.action(editor, { save, restore, updateCode });
}
}
}
};

const on = <K extends keyof HTMLElementEventMap>(type: K, fn: (event: HTMLElementEventMap[K]) => void) => {
listeners.push([type, fn])
editor.addEventListener(type, fn)
Expand All @@ -94,6 +127,7 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
if (event.defaultPrevented) return

prev = toString()
handleKeybindings(event)
if (options.preserveIdent) handleNewLine(event)
else legacyNewLineFix(event)
if (options.catchTab) handleTabCharacters(event)
Expand Down Expand Up @@ -538,15 +572,17 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
return editor.getRootNode().getSelection() as Selection
}

function updateCode(code: string, callOnUpdate: boolean = true) {
editor.textContent = code
doHighlight(editor)
callOnUpdate && onUpdate(code)
}

return {
updateOptions(newOptions: Partial<Options>) {
Object.assign(options, newOptions)
},
updateCode(code: string, callOnUpdate: boolean = true) {
editor.textContent = code
doHighlight(editor)
callOnUpdate && onUpdate(code)
},
updateCode,
onUpdate(callback: (code: string) => void) {
onUpdate = callback
},
Expand Down