(editor: HTMLElement, highlight: (e: HTMLElement, pos?: Position) => void, opt: Partial<Options> = {})
| 33 | export type CodeJar = ReturnType<typeof CodeJar> |
| 34 | |
| 35 | export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: Position) => void, opt: Partial<Options> = {}) { |
| 36 | const options: Options = { |
| 37 | tab: '\t', |
| 38 | indentOn: /[({\[]$/, |
| 39 | moveToNewLine: /^[)}\]]/, |
| 40 | spellcheck: false, |
| 41 | catchTab: true, |
| 42 | preserveIdent: true, |
| 43 | addClosing: true, |
| 44 | history: true, |
| 45 | window: globalWindow, |
| 46 | autoclose: { |
| 47 | open: '([{\'"', |
| 48 | close: ')]}\'"' |
| 49 | }, |
| 50 | ...opt |
| 51 | } |
| 52 | |
| 53 | const window = options.window |
| 54 | const document = window.document |
| 55 | |
| 56 | const listeners: [string, any][] = [] |
| 57 | const history: HistoryRecord[] = [] |
| 58 | let at = -1 |
| 59 | let focus = false |
| 60 | let onUpdate: (code: string) => void | undefined = () => void 0 |
| 61 | let prev: string // code content prior keydown event |
| 62 | |
| 63 | editor.setAttribute('contenteditable', 'plaintext-only') |
| 64 | editor.setAttribute('spellcheck', options.spellcheck ? 'true' : 'false') |
| 65 | editor.style.outline = 'none' |
| 66 | editor.style.overflowWrap = 'break-word' |
| 67 | editor.style.overflowY = 'auto' |
| 68 | editor.style.whiteSpace = 'pre-wrap' |
| 69 | |
| 70 | const doHighlight = (editor: HTMLElement, pos?: Position) => { |
| 71 | highlight(editor, pos) |
| 72 | } |
| 73 | |
| 74 | const matchFirefoxVersion = |
| 75 | window.navigator.userAgent.match(/Firefox\/([0-9]+)\./) |
| 76 | const firefoxVersion = matchFirefoxVersion |
| 77 | ? parseInt(matchFirefoxVersion[1]) |
| 78 | : 0 |
| 79 | let isLegacy = false // true if plaintext-only is not supported |
| 80 | if (editor.contentEditable !== 'plaintext-only' || firefoxVersion >= 136) { isLegacy = true } |
| 81 | if (isLegacy) editor.setAttribute('contenteditable', 'true') |
| 82 | |
| 83 | const debounceHighlight = debounce(() => { |
| 84 | const pos = save() |
| 85 | doHighlight(editor, pos) |
| 86 | restore(pos) |
| 87 | }, 30) |
| 88 | |
| 89 | let recording = false |
| 90 | const shouldRecord = (event: KeyboardEvent): boolean => { |
| 91 | return !isUndo(event) && !isRedo(event) && |
| 92 | event.key !== 'Meta' && |
nothing calls this directly
no test coverage detected