@atlaskit/editor-plugin-panel 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.eslintrc.js +18 -0
  2. package/CHANGELOG.md +1 -0
  3. package/LICENSE.md +13 -0
  4. package/README.md +30 -0
  5. package/dist/cjs/actions.js +125 -0
  6. package/dist/cjs/index.js +13 -0
  7. package/dist/cjs/message.js +44 -0
  8. package/dist/cjs/nodeviews/panel.js +94 -0
  9. package/dist/cjs/plugin.js +215 -0
  10. package/dist/cjs/pm-plugins/keymaps.js +81 -0
  11. package/dist/cjs/pm-plugins/main.js +28 -0
  12. package/dist/cjs/toolbar.js +293 -0
  13. package/dist/cjs/types.js +8 -0
  14. package/dist/cjs/utils.js +53 -0
  15. package/dist/es2019/actions.js +116 -0
  16. package/dist/es2019/index.js +1 -0
  17. package/dist/es2019/message.js +38 -0
  18. package/dist/es2019/nodeviews/panel.js +82 -0
  19. package/dist/es2019/plugin.js +191 -0
  20. package/dist/es2019/pm-plugins/keymaps.js +82 -0
  21. package/dist/es2019/pm-plugins/main.js +21 -0
  22. package/dist/es2019/toolbar.js +279 -0
  23. package/dist/es2019/types.js +2 -0
  24. package/dist/es2019/utils.js +48 -0
  25. package/dist/esm/actions.js +116 -0
  26. package/dist/esm/index.js +1 -0
  27. package/dist/esm/message.js +38 -0
  28. package/dist/esm/nodeviews/panel.js +87 -0
  29. package/dist/esm/plugin.js +208 -0
  30. package/dist/esm/pm-plugins/keymaps.js +74 -0
  31. package/dist/esm/pm-plugins/main.js +22 -0
  32. package/dist/esm/toolbar.js +286 -0
  33. package/dist/esm/types.js +2 -0
  34. package/dist/esm/utils.js +46 -0
  35. package/dist/types/actions.d.ts +12 -0
  36. package/dist/types/index.d.ts +3 -0
  37. package/dist/types/message.d.ts +37 -0
  38. package/dist/types/nodeviews/panel.d.ts +36 -0
  39. package/dist/types/plugin.d.ts +17 -0
  40. package/dist/types/pm-plugins/keymaps.d.ts +3 -0
  41. package/dist/types/pm-plugins/main.d.ts +11 -0
  42. package/dist/types/toolbar.d.ts +15 -0
  43. package/dist/types/types.d.ts +21 -0
  44. package/dist/types/utils.d.ts +5 -0
  45. package/dist/types-ts4.5/actions.d.ts +12 -0
  46. package/dist/types-ts4.5/index.d.ts +3 -0
  47. package/dist/types-ts4.5/message.d.ts +37 -0
  48. package/dist/types-ts4.5/nodeviews/panel.d.ts +36 -0
  49. package/dist/types-ts4.5/plugin.d.ts +17 -0
  50. package/dist/types-ts4.5/pm-plugins/keymaps.d.ts +3 -0
  51. package/dist/types-ts4.5/pm-plugins/main.d.ts +11 -0
  52. package/dist/types-ts4.5/toolbar.d.ts +15 -0
  53. package/dist/types-ts4.5/types.d.ts +21 -0
  54. package/dist/types-ts4.5/utils.d.ts +5 -0
  55. package/package.json +102 -0
  56. package/report.api.md +74 -0
  57. package/tmp/api-report-tmp.d.ts +44 -0
@@ -0,0 +1,116 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ import { PanelType } from '@atlaskit/adf-schema';
5
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
6
+ import { withAnalytics } from '@atlaskit/editor-common/editor-analytics';
7
+ import { getPanelTypeBackgroundNoTokens } from '@atlaskit/editor-common/panel';
8
+ import { wrapSelectionIn } from '@atlaskit/editor-common/utils';
9
+ import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
10
+ import { findParentNodeOfType, findSelectedNodeOfType, removeParentNodeOfType, removeSelectedNode } from '@atlaskit/editor-prosemirror/utils';
11
+ import { findPanel } from './utils';
12
+ export var removePanel = function removePanel(editorAnalyticsAPI) {
13
+ return function (state, dispatch) {
14
+ var nodes = state.schema.nodes,
15
+ tr = state.tr;
16
+ var payload = {
17
+ action: ACTION.DELETED,
18
+ actionSubject: ACTION_SUBJECT.PANEL,
19
+ attributes: {
20
+ inputMethod: INPUT_METHOD.TOOLBAR
21
+ },
22
+ eventType: EVENT_TYPE.TRACK
23
+ };
24
+ var deleteTr = tr;
25
+ if (findSelectedNodeOfType(nodes.panel)(tr.selection)) {
26
+ deleteTr = removeSelectedNode(tr);
27
+ } else if (findParentNodeOfType(nodes.panel)(tr.selection)) {
28
+ deleteTr = removeParentNodeOfType(nodes.panel)(tr);
29
+ }
30
+ if (!deleteTr) {
31
+ return false;
32
+ }
33
+ if (dispatch) {
34
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent(payload)(deleteTr);
35
+ dispatch(deleteTr);
36
+ }
37
+ return true;
38
+ };
39
+ };
40
+ export var changePanelType = function changePanelType(editorAnalyticsAPI) {
41
+ return function (panelType) {
42
+ var panelOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
43
+ var allowCustomPanel = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
44
+ return function (state, dispatch) {
45
+ var nodes = state.schema.nodes,
46
+ tr = state.tr;
47
+ var panelNode = findPanel(state);
48
+ if (panelNode === undefined) {
49
+ return false;
50
+ }
51
+ var newType = panelType;
52
+ var previousType = panelNode.node.attrs.panelType;
53
+ var newTr;
54
+ if (allowCustomPanel) {
55
+ var previousColor = panelNode.node.attrs.panelType === 'custom' ? panelNode.node.attrs.panelColor || 'none' : getPanelTypeBackgroundNoTokens(previousType);
56
+ var previousIcon = panelNode.node.attrs.panelIcon;
57
+ var previousIconId = panelNode.node.attrs.panelIconId;
58
+ var previousIconText = panelNode.node.attrs.panelIconText;
59
+ var newPanelOptions = _objectSpread({
60
+ color: previousColor,
61
+ emoji: previousIcon,
62
+ emojiId: previousIconId,
63
+ emojiText: previousIconText
64
+ }, panelOptions);
65
+ newTr = tr.setNodeMarkup(panelNode.pos, nodes.panel, {
66
+ panelIcon: newPanelOptions.emoji,
67
+ panelIconId: newPanelOptions.emojiId,
68
+ panelIconText: newPanelOptions.emojiText,
69
+ panelColor: newPanelOptions.color,
70
+ panelType: panelType
71
+ });
72
+ } else {
73
+ newTr = tr.setNodeMarkup(panelNode.pos, nodes.panel, {
74
+ panelType: panelType
75
+ });
76
+ }
77
+ var payload = {
78
+ action: ACTION.CHANGED_TYPE,
79
+ actionSubject: ACTION_SUBJECT.PANEL,
80
+ attributes: {
81
+ newType: newType,
82
+ previousType: previousType
83
+ },
84
+ eventType: EVENT_TYPE.TRACK
85
+ };
86
+
87
+ // Select the panel if it was previously selected
88
+ var newTrWithSelection = state.selection instanceof NodeSelection && state.selection.node.type.name === 'panel' ? newTr.setSelection(new NodeSelection(tr.doc.resolve(panelNode.pos))) : newTr;
89
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent(payload)(newTrWithSelection);
90
+ newTrWithSelection.setMeta('scrollIntoView', false);
91
+ if (dispatch) {
92
+ dispatch(newTrWithSelection);
93
+ }
94
+ return true;
95
+ };
96
+ };
97
+ };
98
+ export function insertPanelWithAnalytics(inputMethod, analyticsAPI) {
99
+ return withAnalytics(analyticsAPI, {
100
+ action: ACTION.INSERTED,
101
+ actionSubject: ACTION_SUBJECT.DOCUMENT,
102
+ actionSubjectId: ACTION_SUBJECT_ID.PANEL,
103
+ attributes: {
104
+ inputMethod: inputMethod,
105
+ panelType: PanelType.INFO // only info panels can be inserted via this action
106
+ },
107
+
108
+ eventType: EVENT_TYPE.TRACK
109
+ })(function (state, dispatch) {
110
+ var nodes = state.schema.nodes;
111
+ if (nodes.panel && nodes.paragraph) {
112
+ return wrapSelectionIn(nodes.panel)(state, dispatch);
113
+ }
114
+ return false;
115
+ });
116
+ }
@@ -0,0 +1 @@
1
+ export { default as panelPlugin } from './plugin';
@@ -0,0 +1,38 @@
1
+ import { defineMessages } from 'react-intl-next';
2
+ export var messages = defineMessages({
3
+ info: {
4
+ id: 'fabric.editor.info',
5
+ defaultMessage: 'Info',
6
+ description: 'Panels provide a way to highlight text. The info panel has a blue background.'
7
+ },
8
+ note: {
9
+ id: 'fabric.editor.note',
10
+ defaultMessage: 'Note',
11
+ description: 'Panels provide a way to highlight text. The note panel has a purple background.'
12
+ },
13
+ success: {
14
+ id: 'fabric.editor.success',
15
+ defaultMessage: 'Success',
16
+ description: 'Panels provide a way to highlight text. The success panel has a green background.'
17
+ },
18
+ warning: {
19
+ id: 'fabric.editor.warning',
20
+ defaultMessage: 'Warning',
21
+ description: 'Panels provide a way to highlight text. The warning panel has a yellow background.'
22
+ },
23
+ error: {
24
+ id: 'fabric.editor.error',
25
+ defaultMessage: 'Error',
26
+ description: 'Panels provide a way to highlight text. The error panel has a red background.'
27
+ },
28
+ emoji: {
29
+ id: 'fabric.editor.panel.emoji',
30
+ defaultMessage: 'Add emoji',
31
+ description: 'Select the panel icon'
32
+ },
33
+ backgroundColor: {
34
+ id: 'fabric.editor.panel.backgroundColor',
35
+ defaultMessage: 'Background color',
36
+ description: 'Select the panel background color.'
37
+ }
38
+ });
@@ -0,0 +1,87 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import React from 'react';
4
+ import ReactDOM from 'react-dom';
5
+ import { PanelType } from '@atlaskit/adf-schema';
6
+ import { Emoji } from '@atlaskit/editor-common/emoji';
7
+ import { PanelErrorIcon, PanelInfoIcon, PanelNoteIcon, PanelSuccessIcon, PanelWarningIcon } from '@atlaskit/editor-common/icons';
8
+ import { PanelSharedCssClassName } from '@atlaskit/editor-common/panel';
9
+ import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
10
+ import { akEditorCustomIconSize } from '@atlaskit/editor-shared-styles/consts';
11
+ import TipIcon from '@atlaskit/icon/glyph/editor/hint';
12
+ import { panelAttrsToDom } from '../utils';
13
+ export var panelIcons = {
14
+ info: PanelInfoIcon,
15
+ success: PanelSuccessIcon,
16
+ note: PanelNoteIcon,
17
+ tip: TipIcon,
18
+ warning: PanelWarningIcon,
19
+ error: PanelErrorIcon,
20
+ custom: PanelInfoIcon
21
+ };
22
+ export var PanelIcon = function PanelIcon(props) {
23
+ var allowCustomPanel = props.allowCustomPanel,
24
+ providerFactory = props.providerFactory,
25
+ _props$panelAttribute = props.panelAttributes,
26
+ panelType = _props$panelAttribute.panelType,
27
+ panelIcon = _props$panelAttribute.panelIcon,
28
+ panelIconId = _props$panelAttribute.panelIconId,
29
+ panelIconText = _props$panelAttribute.panelIconText;
30
+ if (allowCustomPanel && panelIcon && panelType === PanelType.CUSTOM) {
31
+ return /*#__PURE__*/React.createElement(Emoji, {
32
+ providers: providerFactory,
33
+ shortName: panelIcon,
34
+ id: panelIconId,
35
+ fallback: panelIconText,
36
+ showTooltip: false,
37
+ allowTextFallback: false,
38
+ fitToHeight: akEditorCustomIconSize
39
+ });
40
+ }
41
+ var Icon = panelIcons[panelType];
42
+ return /*#__PURE__*/React.createElement(Icon, {
43
+ label: "Panel ".concat(panelType)
44
+ });
45
+ };
46
+ var PanelNodeView = /*#__PURE__*/function () {
47
+ function PanelNodeView(node, view, getPos, pluginOptions, providerFactory) {
48
+ _classCallCheck(this, PanelNodeView);
49
+ this.providerFactory = providerFactory;
50
+ this.pluginOptions = pluginOptions;
51
+ this.view = view;
52
+ this.node = node;
53
+ var _DOMSerializer$render = DOMSerializer.renderSpec(document, panelAttrsToDom(node.attrs, pluginOptions.allowCustomPanel || false)),
54
+ dom = _DOMSerializer$render.dom,
55
+ contentDOM = _DOMSerializer$render.contentDOM;
56
+ this.getPos = getPos;
57
+ this.dom = dom;
58
+ this.contentDOM = contentDOM;
59
+ this.icon = this.dom.querySelector(".".concat(PanelSharedCssClassName.icon));
60
+ if (!this.icon) {
61
+ return;
62
+ }
63
+ // set contentEditable as false to be able to select the custom panels with keyboard
64
+ this.icon.contentEditable = 'false';
65
+ ReactDOM.render( /*#__PURE__*/React.createElement(PanelIcon, {
66
+ allowCustomPanel: pluginOptions.allowCustomPanel,
67
+ panelAttributes: node.attrs,
68
+ providerFactory: this.providerFactory
69
+ }), this.icon);
70
+ }
71
+ _createClass(PanelNodeView, [{
72
+ key: "ignoreMutation",
73
+ value: function ignoreMutation(mutation) {
74
+ // ignore mutation if it caused by the icon.
75
+ var isIcon = mutation.target === this.icon || mutation.target.parentNode === this.icon;
76
+ // ignore mutation if it caused by the lazy load emoji inside icon.
77
+ var isInsideIcon = this.icon.contains(mutation.target);
78
+ return isIcon || isInsideIcon;
79
+ }
80
+ }]);
81
+ return PanelNodeView;
82
+ }();
83
+ export var getPanelNodeView = function getPanelNodeView(pluginOptions, providerFactory) {
84
+ return function (node, view, getPos) {
85
+ return new PanelNodeView(node, view, getPos, pluginOptions, providerFactory);
86
+ };
87
+ };
@@ -0,0 +1,208 @@
1
+ import React from 'react';
2
+ import { panel, PanelType } from '@atlaskit/adf-schema';
3
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
4
+ import { blockTypeMessages } from '@atlaskit/editor-common/messages';
5
+ import { IconCustomPanel, IconPanel, IconPanelError, IconPanelNote, IconPanelSuccess, IconPanelWarning } from '@atlaskit/editor-common/quick-insert';
6
+ import { createWrapSelectionTransaction } from '@atlaskit/editor-common/utils';
7
+ import { T50 } from '@atlaskit/theme/colors';
8
+ import { insertPanelWithAnalytics } from './actions';
9
+ import keymap from './pm-plugins/keymaps';
10
+ import { createPlugin } from './pm-plugins/main';
11
+ import { getToolbarConfig } from './toolbar';
12
+ var panelPlugin = function panelPlugin(_ref) {
13
+ var _ref$config = _ref.config,
14
+ options = _ref$config === void 0 ? {} : _ref$config,
15
+ api = _ref.api;
16
+ return {
17
+ name: 'panel',
18
+ nodes: function nodes() {
19
+ var panelNode = panel(!!options.allowCustomPanel);
20
+ return [{
21
+ name: 'panel',
22
+ node: panelNode
23
+ }];
24
+ },
25
+ pmPlugins: function pmPlugins() {
26
+ return [{
27
+ name: 'panel',
28
+ plugin: function plugin(_ref2) {
29
+ var providerFactory = _ref2.providerFactory,
30
+ dispatch = _ref2.dispatch;
31
+ return createPlugin(dispatch, providerFactory, options);
32
+ }
33
+ }, {
34
+ name: 'panelKeyMap',
35
+ plugin: function plugin() {
36
+ return keymap();
37
+ }
38
+ }];
39
+ },
40
+ actions: {
41
+ insertPanel: function insertPanel(inputMethod) {
42
+ var _api$analytics;
43
+ return insertPanelWithAnalytics(inputMethod, api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions);
44
+ }
45
+ },
46
+ pluginsOptions: {
47
+ quickInsert: function quickInsert(_ref3) {
48
+ var formatMessage = _ref3.formatMessage;
49
+ var quickInsertOptions = [{
50
+ id: 'infopanel',
51
+ title: formatMessage(blockTypeMessages.infoPanel),
52
+ keywords: ['panel'],
53
+ description: formatMessage(blockTypeMessages.infoPanelDescription),
54
+ priority: 800,
55
+ icon: function icon() {
56
+ return /*#__PURE__*/React.createElement(IconPanel, null);
57
+ },
58
+ action: function action(insert, state) {
59
+ return createPanelAction({
60
+ state: state,
61
+ attributes: {
62
+ panelType: PanelType.INFO
63
+ },
64
+ api: api
65
+ });
66
+ }
67
+ }, {
68
+ id: 'notepanel',
69
+ title: formatMessage(blockTypeMessages.notePanel),
70
+ description: formatMessage(blockTypeMessages.notePanelDescription),
71
+ priority: 1000,
72
+ icon: function icon() {
73
+ return /*#__PURE__*/React.createElement(IconPanelNote, null);
74
+ },
75
+ action: function action(insert, state) {
76
+ return createPanelAction({
77
+ state: state,
78
+ attributes: {
79
+ panelType: PanelType.NOTE
80
+ },
81
+ api: api
82
+ });
83
+ }
84
+ }, {
85
+ id: 'successpanel',
86
+ title: formatMessage(blockTypeMessages.successPanel),
87
+ description: formatMessage(blockTypeMessages.successPanelDescription),
88
+ keywords: ['tip'],
89
+ priority: 1000,
90
+ icon: function icon() {
91
+ return /*#__PURE__*/React.createElement(IconPanelSuccess, null);
92
+ },
93
+ action: function action(insert, state) {
94
+ return createPanelAction({
95
+ state: state,
96
+ attributes: {
97
+ panelType: PanelType.SUCCESS
98
+ },
99
+ api: api
100
+ });
101
+ }
102
+ }, {
103
+ id: 'warningpanel',
104
+ title: formatMessage(blockTypeMessages.warningPanel),
105
+ description: formatMessage(blockTypeMessages.warningPanelDescription),
106
+ priority: 1000,
107
+ icon: function icon() {
108
+ return /*#__PURE__*/React.createElement(IconPanelWarning, null);
109
+ },
110
+ action: function action(insert, state) {
111
+ return createPanelAction({
112
+ state: state,
113
+ attributes: {
114
+ panelType: PanelType.WARNING
115
+ },
116
+ api: api
117
+ });
118
+ }
119
+ }, {
120
+ id: 'errorpanel',
121
+ title: formatMessage(blockTypeMessages.errorPanel),
122
+ description: formatMessage(blockTypeMessages.errorPanelDescription),
123
+ priority: 1000,
124
+ icon: function icon() {
125
+ return /*#__PURE__*/React.createElement(IconPanelError, null);
126
+ },
127
+ action: function action(insert, state) {
128
+ return createPanelAction({
129
+ state: state,
130
+ attributes: {
131
+ panelType: PanelType.ERROR
132
+ },
133
+ api: api
134
+ });
135
+ }
136
+ }];
137
+ if (options.allowCustomPanel && options.allowCustomPanelEdit) {
138
+ quickInsertOptions.push({
139
+ id: 'custompanel',
140
+ title: formatMessage(blockTypeMessages.customPanel),
141
+ description: formatMessage(blockTypeMessages.customPanelDescription),
142
+ priority: 1000,
143
+ icon: function icon() {
144
+ return /*#__PURE__*/React.createElement(IconCustomPanel, null);
145
+ },
146
+ action: function action(insert, state) {
147
+ return createPanelAction({
148
+ state: state,
149
+ attributes: {
150
+ panelType: PanelType.CUSTOM,
151
+ panelIcon: ':rainbow:',
152
+ panelIconId: '1f308',
153
+ panelIconText: '🌈',
154
+ // TODO: https://product-fabric.atlassian.net/browse/DSP-7268
155
+ // eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage
156
+ panelColor: T50
157
+ },
158
+ api: api
159
+ });
160
+ }
161
+ });
162
+ }
163
+ return quickInsertOptions;
164
+ },
165
+ floatingToolbar: function floatingToolbar(state, intl, providerFactory) {
166
+ return getToolbarConfig(state, intl, options, providerFactory, api);
167
+ }
168
+ }
169
+ };
170
+ };
171
+
172
+ /**
173
+ * Creates panel action and wrap selection transaction with analytics for the panel insertion.
174
+ *
175
+ * @example
176
+ * const tr = createPanelAction({
177
+ * state: editorState,
178
+ * attributes: { panelType: 'info' },
179
+ * });
180
+ * if (tr) {
181
+ * applyTransaction(tr);
182
+ * }
183
+ */
184
+ function createPanelAction(_ref4) {
185
+ var state = _ref4.state,
186
+ attributes = _ref4.attributes,
187
+ api = _ref4.api;
188
+ var tr = createWrapSelectionTransaction({
189
+ state: state,
190
+ type: state.schema.nodes.panel,
191
+ nodeAttributes: attributes
192
+ });
193
+ if (tr) {
194
+ var _api$analytics2;
195
+ api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 || _api$analytics2.actions.attachAnalyticsEvent({
196
+ action: ACTION.INSERTED,
197
+ actionSubject: ACTION_SUBJECT.DOCUMENT,
198
+ actionSubjectId: ACTION_SUBJECT_ID.PANEL,
199
+ attributes: {
200
+ inputMethod: INPUT_METHOD.QUICK_INSERT,
201
+ panelType: attributes.panelType
202
+ },
203
+ eventType: EVENT_TYPE.TRACK
204
+ })(tr);
205
+ }
206
+ return tr;
207
+ }
208
+ export default panelPlugin;
@@ -0,0 +1,74 @@
1
+ import { isEmptyNode } from '@atlaskit/editor-common/utils';
2
+ import { keymap } from '@atlaskit/editor-prosemirror/keymap';
3
+ import { findParentNodeOfType, hasParentNodeOfType, setTextSelection } from '@atlaskit/editor-prosemirror/utils';
4
+ function findParentNode(selection, nodeType) {
5
+ var parentPosition = findParentNodeOfType(nodeType)(selection);
6
+ if (parentPosition) {
7
+ return parentPosition.node;
8
+ }
9
+ return null;
10
+ }
11
+ function isInsideAnEmptyNode(selection, nodeType, schema) {
12
+ var parentNode = findParentNode(selection, nodeType);
13
+ return parentNode && isEmptyNode(schema)(parentNode);
14
+ }
15
+
16
+ // Somewhat broken and subverted: https://product-fabric.atlassian.net/browse/ED-6504
17
+ export function keymapPlugin() {
18
+ var deleteCurrentItem = function deleteCurrentItem($from, tr) {
19
+ return tr.delete($from.before($from.depth) - 1, $from.end($from.depth) + 1);
20
+ };
21
+ var keymaps = {
22
+ Backspace: function Backspace(state, dispatch) {
23
+ var _nodeBeforePanel$type;
24
+ var selection = state.selection,
25
+ nodes = state.schema.nodes,
26
+ tr = state.tr;
27
+ var panel = nodes.panel,
28
+ blockquote = nodes.blockquote;
29
+ var $from = selection.$from,
30
+ $to = selection.$to;
31
+ // Don't do anything if selection is a range
32
+ if ($from.pos !== $to.pos) {
33
+ return false;
34
+ }
35
+
36
+ // If not at the start of a panel, no joining will happen so allow default behaviour (backspacing characters etc..)
37
+ if ($from.parentOffset !== 0) {
38
+ return false;
39
+ }
40
+ var $previousPos = tr.doc.resolve(Math.max(0, $from.before($from.depth) - 1));
41
+ var previousNodeType = $previousPos.pos > 0 && $previousPos.parent && $previousPos.parent.type;
42
+ var parentNodeType = $from.parent.type;
43
+ var isPreviousNodeAPanel = previousNodeType === panel;
44
+ var isParentNodeAPanel = parentNodeType === panel;
45
+ var nodeBeforePanel = $previousPos === null || $previousPos === void 0 ? void 0 : $previousPos.nodeBefore;
46
+
47
+ // Stops merging panels when deleting empty paragraph in between
48
+ // Stops merging blockquotes with panels when deleting from start of blockquote
49
+
50
+ if (isPreviousNodeAPanel && !isParentNodeAPanel || isInsideAnEmptyNode(selection, panel, state.schema) || hasParentNodeOfType(blockquote)(selection) ||
51
+ // Lift line of panel content up and out of the panel, when backspacing
52
+ // at the start of a panel, if the node before the panel is an 'isolating' node
53
+ // (for e.g. a table, or an expand), otherwise the default prosemirror backspace
54
+ // behaviour will fallback to 'select node backward' logic because the node
55
+ // before is an isolating node.
56
+ nodeBeforePanel !== null && nodeBeforePanel !== void 0 && (_nodeBeforePanel$type = nodeBeforePanel.type) !== null && _nodeBeforePanel$type !== void 0 && (_nodeBeforePanel$type = _nodeBeforePanel$type.spec) !== null && _nodeBeforePanel$type !== void 0 && _nodeBeforePanel$type.isolating && hasParentNodeOfType(panel)(selection)) {
57
+ var content = $from.node($from.depth).content;
58
+ var insertPos = $previousPos.pos;
59
+ deleteCurrentItem($from, tr).insert(insertPos, content);
60
+ if (dispatch) {
61
+ dispatch(setTextSelection(insertPos)(tr).scrollIntoView());
62
+ }
63
+ return true;
64
+ }
65
+ var nodeType = $from.node().type;
66
+ if (nodeType !== panel) {
67
+ return false;
68
+ }
69
+ return true;
70
+ }
71
+ };
72
+ return keymap(keymaps);
73
+ }
74
+ export default keymapPlugin;
@@ -0,0 +1,22 @@
1
+ import { PanelSharedCssClassName } from '@atlaskit/editor-common/panel';
2
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
3
+ import { createSelectionClickHandler } from '@atlaskit/editor-common/selection';
4
+ import { getPanelNodeView } from '../nodeviews/panel';
5
+ import { pluginKey } from '../types';
6
+ export var createPlugin = function createPlugin(dispatch, providerFactory, pluginOptions) {
7
+ var _pluginOptions$useLon = pluginOptions.useLongPressSelection,
8
+ useLongPressSelection = _pluginOptions$useLon === void 0 ? false : _pluginOptions$useLon;
9
+ return new SafePlugin({
10
+ key: pluginKey,
11
+ props: {
12
+ nodeViews: {
13
+ panel: getPanelNodeView(pluginOptions, providerFactory)
14
+ },
15
+ handleClickOn: createSelectionClickHandler(['panel'], function (target) {
16
+ return !!target.closest(".".concat(PanelSharedCssClassName.prefix));
17
+ }, {
18
+ useLongPressSelection: useLongPressSelection
19
+ })
20
+ }
21
+ });
22
+ };