From 9ef1e983ff5fab4e8b02f03bb235460d9ddc4f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Sat, 29 Sep 2018 12:55:57 +0200 Subject: [PATCH] refactoring --- .../src/plugins/Suggestions.js | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/packages/tiptap-extensions/src/plugins/Suggestions.js b/packages/tiptap-extensions/src/plugins/Suggestions.js index a2844d38..2fb9ce0c 100644 --- a/packages/tiptap-extensions/src/plugins/Suggestions.js +++ b/packages/tiptap-extensions/src/plugins/Suggestions.js @@ -103,7 +103,20 @@ export default function SuggestionsPlugin({ const started = !prev.active && next.active const stopped = prev.active && !next.active const changed = !started && !stopped && prev.text !== next.text - const decorationNode = document.querySelector(`[data-decoration-id="${next.decorationId}"]`) + const handleStart = started || moved + const handleChange = changed && !moved + const handleExit = stopped || moved + + // Cancel when suggestion isn't active + if (!handleStart && !handleChange && !handleExit) { + return + } + + const state = handleExit ? prev : next + const decorationNode = document.querySelector(`[data-decoration-id="${state.decorationId}"]`) + + // build a virtual node for popper.js or tippy.js + // this can be used for building popups without a DOM node const virtualNode = decorationNode ? { getBoundingClientRect() { return decorationNode.getBoundingClientRect() @@ -112,48 +125,34 @@ export default function SuggestionsPlugin({ clientHeight: decorationNode.clientHeight, } : null + const props = { + view, + range: state.range, + query: state.text, + text: state.fullText, + decorationNode, + virtualNode, + items: onFilter(items, state.text), + command: ({ range, attrs }) => { + command({ + range, + attrs, + schema: view.state.schema, + })(view.state, view.dispatch, view) + }, + } + // Trigger the hooks when necessary - if (stopped || moved) { - onExit({ - view, - range: prev.range, - query: prev.text, - text: prev.fullText, - decorationNode, - virtualNode, - items: onFilter(items, prev.text), - }) + if (handleExit) { + onExit(props) } - if (changed && !moved) { - onChange({ - view, - range: next.range, - query: next.text, - text: next.fullText, - decorationNode, - virtualNode, - items: onFilter(items, next.text), - }) + if (handleChange) { + onChange(props) } - if (started || moved) { - onEnter({ - view, - range: next.range, - query: next.text, - text: next.fullText, - decorationNode, - virtualNode, - items: onFilter(items, next.text), - command: ({ range, attrs }) => { - command({ - range, - attrs, - schema: view.state.schema, - })(view.state, view.dispatch, view) - }, - }) + if (handleStart) { + onEnter(props) } }, };