import * as React from 'react'; import { useState, useEffect, useCallback, useRef, forwardRef, useImperativeHandle } from 'react'; import { ScrollOptions, ScrollOptionTypes, EditorCommand, NoteBodyEditorProps } from '../../utils/types'; import { resourcesStatus, commandAttachFileToBody, handlePasteEvent } from '../../utils/resourceHandling'; import useScroll from './utils/useScroll'; import styles_ from './styles'; import CommandService from '@joplin/lib/services/CommandService'; import { ToolbarButtonInfo } from '@joplin/lib/services/commands/ToolbarButtonUtils'; import ToggleEditorsButton, { Value as ToggleEditorsButtonValue } from '../../../ToggleEditorsButton/ToggleEditorsButton'; import ToolbarButton from '../../../../gui/ToolbarButton/ToolbarButton'; import usePluginServiceRegistration from '../../utils/usePluginServiceRegistration'; import { utils as pluginUtils } from '@joplin/lib/services/plugins/reducer'; import { _, closestSupportedLocale } from '@joplin/lib/locale'; import useContextMenu from './utils/useContextMenu'; import shim from '@joplin/lib/shim'; const { MarkupToHtml } = require('@joplin/renderer'); const taboverride = require('taboverride'); import { reg } from '@joplin/lib/registry'; import BaseItem from '@joplin/lib/models/BaseItem'; const { themeStyle } = require('@joplin/lib/theme'); const { clipboard } = require('electron'); const supportedLocales = require('./supportedLocales'); function markupRenderOptions(override: any = null) { return { plugins: { checkbox: { checkboxRenderingType: 2, }, link_open: { linkRenderingType: 2, }, }, replaceResourceInternalToExternalLinks: true, ...override, }; } function findBlockSource(node: any) { const sources = node.getElementsByClassName('joplin-source'); if (!sources.length) throw new Error('No source for node'); const source = sources[0]; return { openCharacters: source.getAttribute('data-joplin-source-open'), closeCharacters: source.getAttribute('data-joplin-source-close'), content: source.textContent, node: source, language: source.getAttribute('data-joplin-language') || '', }; } function newBlockSource(language: string = '', content: string = ''): any { const fence = language === 'katex' ? '$$' : '```'; const fenceLanguage = language === 'katex' ? '' : language; return { openCharacters: `\n${fence}${fenceLanguage}\n`, closeCharacters: `\n${fence}\n`, content: content, node: null, language: language, }; } function findEditableContainer(node: any): any { while (node) { if (node.classList && node.classList.contains('joplin-editable')) return node; node = node.parentNode; } return null; } function editableInnerHtml(html: string): string { const temp = document.createElement('div'); temp.innerHTML = html; const editable = temp.getElementsByClassName('joplin-editable'); if (!editable.length) throw new Error(`Invalid joplin-editable: ${html}`); return editable[0].innerHTML; } function dialogTextArea_keyDown(event: any) { if (event.key === 'Tab') { window.requestAnimationFrame(() => event.target.focus()); } } let markupToHtml_ = new MarkupToHtml(); function stripMarkup(markupLanguage: number, markup: string, options: any = null) { if (!markupToHtml_) markupToHtml_ = new MarkupToHtml(); return markupToHtml_.stripMarkup(markupLanguage, markup, options); } // Allows pressing tab in a textarea to input an actual tab (instead of changing focus) // taboverride will take care of actually inserting the tab character, while the keydown // event listener will override the default behaviour, which is to focus the next field. function enableTextAreaTab(enable: boolean) { const textAreas = document.getElementsByClassName('tox-textarea'); for (const textArea of textAreas) { taboverride.set(textArea, enable); if (enable) { textArea.addEventListener('keydown', dialogTextArea_keyDown); } else { textArea.removeEventListener('keydown', dialogTextArea_keyDown); } } } interface TinyMceCommand { name: string; value?: any; ui?: boolean; } interface JoplinCommandToTinyMceCommands { [key: string]: TinyMceCommand; } const joplinCommandToTinyMceCommands: JoplinCommandToTinyMceCommands = { 'textBold': { name: 'mceToggleFormat', value: 'bold' }, 'textItalic': { name: 'mceToggleFormat', value: 'italic' }, 'textLink': { name: 'mceLink' }, 'search': { name: 'SearchReplace' }, }; let loadedCssFiles_: string[] = []; let loadedJsFiles_: string[] = []; let dispatchDidUpdateIID_: any = null; let changeId_: number = 1; const TinyMCE = (props: NoteBodyEditorProps, ref: any) => { const [editor, setEditor] = useState(null); const [scriptLoaded, setScriptLoaded] = useState(false); const [editorReady, setEditorReady] = useState(false); const [draggingStarted, setDraggingStarted] = useState(false); const props_onMessage = useRef(null); props_onMessage.current = props.onMessage; const props_onDrop = useRef(null); props_onDrop.current = props.onDrop; const markupToHtml = useRef(null); markupToHtml.current = props.markupToHtml; const lastOnChangeEventInfo = useRef({ content: null, resourceInfos: null, contentKey: null, }); const rootIdRef = useRef(`tinymce-${Date.now()}${Math.round(Math.random() * 10000)}`); const editorRef = useRef(null); editorRef.current = editor; const styles = styles_(props); // const theme = themeStyle(props.themeId); const { scrollToPercent } = useScroll({ editor, onScroll: props.onScroll }); usePluginServiceRegistration(ref); useContextMenu(editor, props.plugins); const dispatchDidUpdate = (editor: any) => { if (dispatchDidUpdateIID_) shim.clearTimeout(dispatchDidUpdateIID_); dispatchDidUpdateIID_ = shim.setTimeout(() => { dispatchDidUpdateIID_ = null; if (editor && editor.getDoc()) editor.getDoc().dispatchEvent(new Event('joplin-noteDidUpdate')); }, 10); }; const insertResourcesIntoContent = useCallback(async (filePaths: string[] = null, options: any = null) => { const resourceMd = await commandAttachFileToBody('', filePaths, options); if (!resourceMd) return; const result = await props.markupToHtml(MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN, resourceMd, markupRenderOptions({ bodyOnly: true })); editor.insertContent(result.html); // editor.fire('joplinChange'); // dispatchDidUpdate(editor); }, [props.markupToHtml, editor]); const insertResourcesIntoContentRef = useRef(null); insertResourcesIntoContentRef.current = insertResourcesIntoContent; const onEditorContentClick = useCallback((event: any) => { const nodeName = event.target ? event.target.nodeName : ''; if (nodeName === 'INPUT' && event.target.getAttribute('type') === 'checkbox') { editor.fire('joplinChange'); dispatchDidUpdate(editor); } if (nodeName === 'A' && (event.ctrlKey || event.metaKey)) { const href = event.target.getAttribute('href'); if (href.indexOf('#') === 0) { const anchorName = href.substr(1); const anchor = editor.getDoc().getElementById(anchorName); if (anchor) { anchor.scrollIntoView(); } else { reg.logger().warn('TinyMce: could not find anchor with ID ', anchorName); } } else { props.onMessage({ channel: href }); } } }, [editor, props.onMessage]); useImperativeHandle(ref, () => { return { content: async () => { if (!editorRef.current) return ''; return prop_htmlToMarkdownRef.current(props.contentMarkupLanguage, editorRef.current.getContent(), props.contentOriginalCss); }, resetScroll: () => { if (editor) editor.getWin().scrollTo(0,0); }, scrollTo: (options: ScrollOptions) => { if (!editor) return; if (options.type === ScrollOptionTypes.Hash) { const anchor = editor.getDoc().getElementById(options.value); if (!anchor) { console.warn('Cannot find hash', options); return; } anchor.scrollIntoView(); } else if (options.type === ScrollOptionTypes.Percent) { scrollToPercent(options.value); } else { throw new Error(`Unsupported scroll options: ${options.type}`); } }, supportsCommand: (name: string) => { // TODO: should also handle commands that are not in this map (insertText, focus, etc); return !!joplinCommandToTinyMceCommands[name]; }, execCommand: async (cmd: EditorCommand) => { if (!editor) return false; reg.logger().debug('TinyMce: execCommand', cmd); let commandProcessed = true; if (cmd.name === 'insertText') { const result = await markupToHtml.current(MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN, cmd.value, { bodyOnly: true }); editor.insertContent(result.html); } else if (cmd.name === 'editor.focus') { editor.focus(); } else if (cmd.name === 'dropItems') { if (cmd.value.type === 'notes') { const result = await markupToHtml.current(MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN, cmd.value.markdownTags.join('\n'), markupRenderOptions({ bodyOnly: true })); editor.insertContent(result.html); } else if (cmd.value.type === 'files') { insertResourcesIntoContentRef.current(cmd.value.paths, { createFileURL: !!cmd.value.createFileURL }); } else { reg.logger().warn('TinyMCE: unsupported drop item: ', cmd); } } else { commandProcessed = false; } if (commandProcessed) return true; const additionalCommands: any = { selectedText: () => { return stripMarkup(MarkupToHtml.MARKUP_LANGUAGE_HTML, editor.selection.getContent()); }, selectedHtml: () => { return editor.selection.getContent(); }, replaceSelection: (value: any) => { editor.selection.setContent(value); editor.fire('joplinChange'); dispatchDidUpdate(editor); // It doesn't make sense but it seems calling setContent // doesn't create an undo step so we need to call it // manually. // https://github.com/tinymce/tinymce/issues/3745 window.requestAnimationFrame(() => editor.undoManager.add()); }, }; if (additionalCommands[cmd.name]) { return additionalCommands[cmd.name](cmd.value); } if (!joplinCommandToTinyMceCommands[cmd.name]) { reg.logger().warn('TinyMCE: unsupported Joplin command: ', cmd); return false; } const tinyMceCmd: TinyMceCommand = { ...joplinCommandToTinyMceCommands[cmd.name] }; if (!('ui' in tinyMceCmd)) tinyMceCmd.ui = false; if (!('value' in tinyMceCmd)) tinyMceCmd.value = null; editor.execCommand(tinyMceCmd.name, tinyMceCmd.ui, tinyMceCmd.value); return true; }, }; }, [editor, props.contentMarkupLanguage, props.contentOriginalCss]); // ----------------------------------------------------------------------------------------- // Load the TinyMCE library. The lib loads additional JS and CSS files on startup // (for themes), and so it needs to be loaded via