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