(editor: Ref<any>)
| 18 | } |
| 19 | |
| 20 | export default function useInlineCode(editor: Ref<any>): UsableInlineCode { |
| 21 | let keydownHandler: ((event: KeyboardEvent) => void) | null = null; |
| 22 | |
| 23 | const inlineCodeButton: InlineCodeButton = { |
| 24 | tooltip: i18n.global.t('wysiwyg_options.codeblock'), |
| 25 | icon: 'code-sample', |
| 26 | |
| 27 | onAction: () => { |
| 28 | editor.value.execCommand('mceToggleFormat', false, 'code'); |
| 29 | }, |
| 30 | |
| 31 | onSetup: (api) => { |
| 32 | const updateActiveState = () => { |
| 33 | const isInlineCode = editor.value.formatter.match('code'); |
| 34 | |
| 35 | api.setActive(isInlineCode); |
| 36 | }; |
| 37 | |
| 38 | updateActiveState(); |
| 39 | |
| 40 | const codeFormatChangedUnbind = editor.value.formatter.formatChanged('code', updateActiveState).unbind; |
| 41 | |
| 42 | keydownHandler = (event: KeyboardEvent) => { |
| 43 | const currentNode = editor.value.selection.getNode(); |
| 44 | if (!isInlineCode(currentNode)) return; |
| 45 | |
| 46 | if (event.key === 'Enter') { |
| 47 | event.preventDefault(); |
| 48 | |
| 49 | editor.value.execCommand('removeformat'); |
| 50 | updateActiveState(); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | editor.value.on('keydown', keydownHandler); |
| 55 | |
| 56 | return () => { |
| 57 | if (codeFormatChangedUnbind) codeFormatChangedUnbind(); |
| 58 | if (keydownHandler) editor.value.off('keydown', keydownHandler); |
| 59 | }; |
| 60 | }, |
| 61 | }; |
| 62 | |
| 63 | return { inlineCodeButton }; |
| 64 | } |
nothing calls this directly
no test coverage detected