1 | import React, { useLayoutEffect } from 'react';
|
2 | import { INPUT_METHOD } from '@atlaskit/editor-common/analytics';
|
3 | import { useSharedPluginState } from '@atlaskit/editor-common/hooks';
|
4 | import { WithProviders } from '@atlaskit/editor-common/provider-factory';
|
5 | import { ToolbarSize } from '@atlaskit/editor-common/types';
|
6 | import { BLOCK_QUOTE, CODE_BLOCK, PANEL } from '@atlaskit/editor-plugin-block-type/consts';
|
7 | import ToolbarInsertBlock from './ui/ToolbarInsertBlock';
|
8 | const toolbarSizeToButtons = toolbarSize => {
|
9 | switch (toolbarSize) {
|
10 | case ToolbarSize.XXL:
|
11 | case ToolbarSize.XL:
|
12 | case ToolbarSize.L:
|
13 | case ToolbarSize.M:
|
14 | return 7;
|
15 | case ToolbarSize.S:
|
16 | return 2;
|
17 | default:
|
18 | return 0;
|
19 | }
|
20 | };
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function handleInsertBlockType(insertCodeBlock, insertPanel, insertBlockQuote) {
|
26 | return name => {
|
27 | if (name === CODE_BLOCK.name && insertCodeBlock) {
|
28 | return insertCodeBlock(INPUT_METHOD.TOOLBAR);
|
29 | }
|
30 | if (name === PANEL.name && insertPanel) {
|
31 | return insertPanel(INPUT_METHOD.TOOLBAR);
|
32 | }
|
33 | if (name === BLOCK_QUOTE.name && insertBlockQuote) {
|
34 | return insertBlockQuote(INPUT_METHOD.TOOLBAR);
|
35 | }
|
36 | return () => false;
|
37 | };
|
38 | }
|
39 | export const insertBlockPlugin = ({
|
40 | config: options = {},
|
41 | api
|
42 | }) => {
|
43 | const toggleDropdownMenuOptionsRef = {
|
44 | current: null
|
45 | };
|
46 | const registerToggleDropdownMenuOptions = cb => {
|
47 | toggleDropdownMenuOptionsRef.current = cb;
|
48 | return () => {
|
49 | toggleDropdownMenuOptionsRef.current = null;
|
50 | };
|
51 | };
|
52 | return {
|
53 | name: 'insertBlock',
|
54 | actions: {
|
55 | toggleAdditionalMenu: () => {
|
56 | const toggle = toggleDropdownMenuOptionsRef.current;
|
57 | if (!toggle) {
|
58 | return;
|
59 | }
|
60 | toggle();
|
61 | }
|
62 | },
|
63 | usePluginHook: () => {
|
64 | useLayoutEffect(() => {
|
65 | return () => {
|
66 | toggleDropdownMenuOptionsRef.current = null;
|
67 | };
|
68 | }, []);
|
69 | },
|
70 | primaryToolbarComponent({
|
71 | editorView,
|
72 | editorActions,
|
73 | dispatchAnalyticsEvent,
|
74 | providerFactory,
|
75 | popupsMountPoint,
|
76 | popupsBoundariesElement,
|
77 | popupsScrollableElement,
|
78 | toolbarSize,
|
79 | disabled,
|
80 | isToolbarReducedSpacing,
|
81 | isLastItem
|
82 | }) {
|
83 | const renderNode = providers => {
|
84 | return React.createElement(ToolbarInsertBlockWithInjectionApi, {
|
85 | pluginInjectionApi: api,
|
86 | editorView: editorView,
|
87 | editorActions: editorActions,
|
88 | dispatchAnalyticsEvent: dispatchAnalyticsEvent,
|
89 | providerFactory: providerFactory,
|
90 | popupsMountPoint: popupsMountPoint,
|
91 | popupsBoundariesElement: popupsBoundariesElement,
|
92 | popupsScrollableElement: popupsScrollableElement,
|
93 | toolbarSize: toolbarSize,
|
94 | disabled: disabled,
|
95 | isToolbarReducedSpacing: isToolbarReducedSpacing,
|
96 | isLastItem: isLastItem,
|
97 | providers: providers,
|
98 | options: options,
|
99 | registerToggleDropdownMenuOptions: registerToggleDropdownMenuOptions
|
100 | });
|
101 | };
|
102 | return React.createElement(WithProviders, {
|
103 | providerFactory: providerFactory,
|
104 | providers: ['emojiProvider'],
|
105 | renderNode: renderNode
|
106 | });
|
107 | }
|
108 | };
|
109 | };
|
110 | function ToolbarInsertBlockWithInjectionApi({
|
111 | editorView,
|
112 | editorActions,
|
113 | dispatchAnalyticsEvent,
|
114 | popupsMountPoint,
|
115 | popupsBoundariesElement,
|
116 | popupsScrollableElement,
|
117 | toolbarSize,
|
118 | disabled,
|
119 | isToolbarReducedSpacing,
|
120 | isLastItem,
|
121 | providers,
|
122 | pluginInjectionApi,
|
123 | options,
|
124 | registerToggleDropdownMenuOptions
|
125 | }) {
|
126 | var _ref, _ref2, _pluginInjectionApi$i, _pluginInjectionApi$c, _pluginInjectionApi$p, _pluginInjectionApi$b, _pluginInjectionApi$e;
|
127 | const buttons = toolbarSizeToButtons(toolbarSize);
|
128 | const {
|
129 | dateState,
|
130 | hyperlinkState,
|
131 | imageUploadState,
|
132 | mentionState,
|
133 | emojiState,
|
134 | blockTypeState,
|
135 | mediaState,
|
136 | typeAheadState,
|
137 | placeholderTextState
|
138 | } = useSharedPluginState(pluginInjectionApi, ['hyperlink', 'date', 'imageUpload', 'mention', 'emoji', 'blockType', 'media', 'typeAhead', 'placeholderText']);
|
139 | return React.createElement(ToolbarInsertBlock, {
|
140 | pluginInjectionApi: pluginInjectionApi,
|
141 | buttons: buttons,
|
142 | isReducedSpacing: isToolbarReducedSpacing,
|
143 | isDisabled: disabled,
|
144 | isTypeAheadAllowed: Boolean(typeAheadState === null || typeAheadState === void 0 ? void 0 : typeAheadState.isAllowed),
|
145 | editorView: editorView,
|
146 | tableSupported: !!editorView.state.schema.nodes.table,
|
147 | tableSelectorSupported: options.tableSelectorSupported && !!editorView.state.schema.nodes.table,
|
148 | actionSupported: !!editorView.state.schema.nodes.taskItem,
|
149 | mentionsSupported: !!(mentionState && mentionState.mentionProvider),
|
150 | mentionsDisabled: !!(mentionState && !mentionState.canInsertMention),
|
151 | decisionSupported: !!editorView.state.schema.nodes.decisionItem,
|
152 | dateEnabled: !!dateState,
|
153 | placeholderTextEnabled: placeholderTextState && placeholderTextState.allowInserting,
|
154 | layoutSectionEnabled: Boolean(pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : pluginInjectionApi.layout),
|
155 | expandEnabled: !!options.allowExpand,
|
156 | mediaUploadsEnabled: (_ref = mediaState && mediaState.allowsUploads) !== null && _ref !== void 0 ? _ref : undefined,
|
157 | onShowMediaPicker: (_ref2 = mediaState && mediaState.showMediaPicker) !== null && _ref2 !== void 0 ? _ref2 : undefined,
|
158 | mediaSupported: !!mediaState,
|
159 | imageUploadSupported: !!(pluginInjectionApi !== null && pluginInjectionApi !== void 0 && pluginInjectionApi.imageUpload),
|
160 | imageUploadEnabled: imageUploadState === null || imageUploadState === void 0 ? void 0 : imageUploadState.enabled,
|
161 | handleImageUpload: pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : (_pluginInjectionApi$i = pluginInjectionApi.imageUpload) === null || _pluginInjectionApi$i === void 0 ? void 0 : _pluginInjectionApi$i.actions.startUpload,
|
162 | availableWrapperBlockTypes: blockTypeState && blockTypeState.availableWrapperBlockTypes,
|
163 | linkSupported: !!hyperlinkState,
|
164 | linkDisabled: !hyperlinkState || !hyperlinkState.canInsertLink || !!hyperlinkState.activeLinkMark,
|
165 | emojiDisabled: !emojiState || !providers.emojiProvider,
|
166 | emojiProvider: providers.emojiProvider,
|
167 | nativeStatusSupported: options.nativeStatusSupported,
|
168 | horizontalRuleEnabled: options.horizontalRuleEnabled,
|
169 | onInsertBlockType: handleInsertBlockType(pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : (_pluginInjectionApi$c = pluginInjectionApi.codeBlock) === null || _pluginInjectionApi$c === void 0 ? void 0 : _pluginInjectionApi$c.actions.insertCodeBlock, pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : (_pluginInjectionApi$p = pluginInjectionApi.panel) === null || _pluginInjectionApi$p === void 0 ? void 0 : _pluginInjectionApi$p.actions.insertPanel, pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : (_pluginInjectionApi$b = pluginInjectionApi.blockType) === null || _pluginInjectionApi$b === void 0 ? void 0 : _pluginInjectionApi$b.actions.insertBlockQuote),
|
170 | onInsertMacroFromMacroBrowser: pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : (_pluginInjectionApi$e = pluginInjectionApi.extension) === null || _pluginInjectionApi$e === void 0 ? void 0 : _pluginInjectionApi$e.actions.insertMacroFromMacroBrowser,
|
171 | popupsMountPoint: popupsMountPoint,
|
172 | popupsBoundariesElement: popupsBoundariesElement,
|
173 | popupsScrollableElement: popupsScrollableElement,
|
174 | insertMenuItems: options.insertMenuItems,
|
175 | editorActions: editorActions,
|
176 | dispatchAnalyticsEvent: dispatchAnalyticsEvent,
|
177 | replacePlusMenuWithElementBrowser: options.replacePlusMenuWithElementBrowser,
|
178 | showElementBrowserLink: options.showElementBrowserLink,
|
179 | showSeparator: !isLastItem && toolbarSize <= ToolbarSize.S,
|
180 | registerToggleDropdownMenuOptions: registerToggleDropdownMenuOptions
|
181 | });
|
182 | } |
\ | No newline at end of file |