diff --git a/apps/editor/src/editorCore.ts b/apps/editor/src/editorCore.ts index 84d86ef812..6f16285201 100644 --- a/apps/editor/src/editorCore.ts +++ b/apps/editor/src/editorCore.ts @@ -42,6 +42,7 @@ import { cls, replaceBRWithEmptyBlock } from './utils/dom'; import { sanitizeHTML } from './sanitizer/htmlSanitizer'; import { createHTMLSchemaMap } from './wysiwyg/nodes/html'; import { getHTMLRenderConvertors } from './markdown/htmlRenderConvertors'; +import { buildQuery } from './queries/queryManager'; /** * ToastUIEditorCore @@ -273,6 +274,7 @@ class ToastUIEditorCore { this.scrollSync = new ScrollSync(this.mdEditor, this.preview, this.eventEmitter); this.addInitEvent(); this.addInitCommand(mdCommands, wwCommands); + buildQuery(this); if (this.options.hooks) { forEachOwnProperties(this.options.hooks, (fn, key) => this.addHook(key, fn)); diff --git a/apps/editor/src/event/eventEmitter.ts b/apps/editor/src/event/eventEmitter.ts index 1b83688002..337dff1424 100644 --- a/apps/editor/src/event/eventEmitter.ts +++ b/apps/editor/src/event/eventEmitter.ts @@ -22,6 +22,7 @@ const eventTypeList: EventTypes[] = [ 'mixinTableOffsetMapPrototype', 'setFocusedNode', 'removePopupWidget', + 'query', // provide event for user 'openPopup', 'closePopup', diff --git a/apps/editor/src/markdown/mdPreview.ts b/apps/editor/src/markdown/mdPreview.ts index add3c182cd..fb97117f2b 100644 --- a/apps/editor/src/markdown/mdPreview.ts +++ b/apps/editor/src/markdown/mdPreview.ts @@ -232,7 +232,7 @@ class MarkdownPreview { } getElement() { - return this.el; + return this.el!; } getHTML() { diff --git a/apps/editor/src/queries/queryManager.ts b/apps/editor/src/queries/queryManager.ts new file mode 100644 index 0000000000..0bdb54c3e2 --- /dev/null +++ b/apps/editor/src/queries/queryManager.ts @@ -0,0 +1,17 @@ +import type { EditorCore as Editor } from '@t/editor'; + +type QueryFn = (editor: Editor, payload?: Record) => any; + +const queryMap: Record = { + getPopupInitialValues(editor, payload) { + const { popupName } = payload!; + + return popupName === 'link' ? { linkText: editor.getSelectedText() } : {}; + }, +}; + +export function buildQuery(editor: Editor) { + editor.eventEmitter.listen('query', (query: string, payload?: Record) => + queryMap[query](editor, payload) + ); +} diff --git a/apps/editor/src/ui/components/toolbar/toolbarButton.ts b/apps/editor/src/ui/components/toolbar/toolbarButton.ts index fda07e0efc..415ed467a3 100644 --- a/apps/editor/src/ui/components/toolbar/toolbarButton.ts +++ b/apps/editor/src/ui/components/toolbar/toolbarButton.ts @@ -54,17 +54,20 @@ export class ToolbarButtonComp extends Component { }; private execCommand = () => { - const { item, execCommand, setPopupInfo, getBound } = this.props; + const { item, execCommand, setPopupInfo, getBound, eventEmitter } = this.props; const { command, name, popup } = item; if (command) { execCommand(command); } else { const popupName = popup ? 'customPopupBody' : name; + const [initialValues] = eventEmitter.emit('query', 'getPopupInitialValues', { popupName }); + const info = createPopupInfo(popupName, { el: this.refs.el, pos: getBound(this.refs.el), popup, + initialValues, }); if (info) { diff --git a/apps/editor/types/event.d.ts b/apps/editor/types/event.d.ts index 4660134c69..da6183edd6 100644 --- a/apps/editor/types/event.d.ts +++ b/apps/editor/types/event.d.ts @@ -38,6 +38,7 @@ export type EventTypes = | 'mixinTableOffsetMapPrototype' | 'setFocusedNode' | 'removePopupWidget' + | 'query' // provide event for user | 'openPopup' | 'closePopup'