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 { menuItems, ContextMenuOptions, ContextMenuItemType } from '../../utils/contextMenu'; const { MarkupToHtml } = require('lib/joplin-renderer'); const taboverride = require('taboverride'); const { reg } = require('lib/registry.js'); const { _, closestSupportedLocale } = require('lib/locale'); const BaseItem = require('lib/models/BaseItem'); const Resource = require('lib/models/Resource'); const { themeStyle, buildStyle } = require('lib/theme'); const { clipboard } = require('electron'); const supportedLocales = require('./supportedLocales'); function markupRenderOptions(override:any = null) { return { plugins: { checkbox: { renderingType: 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()); } } // 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' }, }; function styles_(props:NoteBodyEditorProps) { return buildStyle('TinyMCE', props.theme, (/* theme:any */) => { return { disabledOverlay: { zIndex: 10, position: 'absolute', backgroundColor: 'white', opacity: 0.7, height: '100%', display: 'flex', flexDirection: 'column', alignItems: 'center', padding: 20, paddingTop: 50, textAlign: 'center', width: '100%', }, rootStyle: { position: 'relative', ...props.style, }, }; }); } 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 contextMenuActionOptions = useRef(null); const markupToHtml = useRef(null); markupToHtml.current = props.markupToHtml; const lastOnChangeEventInfo = useRef({ content: null, resourceInfos: 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.theme); const { scrollToPercent } = useScroll({ editor, onScroll: props.onScroll }); const dispatchDidUpdate = (editor:any) => { if (dispatchDidUpdateIID_) clearTimeout(dispatchDidUpdateIID_); dispatchDidUpdateIID_ = 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 === '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('AceEditor: unsupported drop item: ', cmd); } } else { commandProcessed = false; } if (commandProcessed) return true; 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