-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ContextToolbarLookup.ts
36 lines (29 loc) · 1.33 KB
/
ContextToolbarLookup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*/
import { Toolbar } from '@ephox/bridge';
import { Option, Options } from '@ephox/katamari';
import { Element, TransformFind } from '@ephox/sugar';
import Editor from 'tinymce/core/api/Editor';
import { ScopedToolbars } from './ContextToolbarScopes';
const matchTargetWith = (elem: Element, toolbars: Array<Toolbar.ContextToolbar | Toolbar.ContextForm>): Option<{ toolbarApi: Toolbar.ContextToolbar | Toolbar.ContextForm, elem: Element }> => {
return Options.findMap(toolbars, (toolbarApi) =>
toolbarApi.predicate(elem.dom()) ? Option.some({ toolbarApi, elem }) : Option.none());
};
const lookup = (scopes: ScopedToolbars, editor: Editor) => {
const isRoot = (elem) => elem.dom() === editor.getBody();
const startNode = Element.fromDom(editor.selection.getNode());
return matchTargetWith(startNode, scopes.inNodeScope).orThunk(() => {
return matchTargetWith(startNode, scopes.inEditorScope).orThunk(() => {
return TransformFind.ancestor(startNode, (elem) => {
return matchTargetWith(elem, scopes.inNodeScope);
}, isRoot);
});
});
};
export default {
lookup
};