@atlaskit/editor-plugin-breakout 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 (60) 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/commands/remove-breakout.js +30 -0
  6. package/dist/cjs/commands/set-breakout-mode.js +29 -0
  7. package/dist/cjs/index.js +12 -0
  8. package/dist/cjs/plugin-key.js +11 -0
  9. package/dist/cjs/plugin.js +218 -0
  10. package/dist/cjs/types.js +5 -0
  11. package/dist/cjs/ui/LayoutButton.js +114 -0
  12. package/dist/cjs/utils/find-breakout-node.js +42 -0
  13. package/dist/cjs/utils/get-breakout-mode.js +24 -0
  14. package/dist/cjs/utils/is-breakout-mark-allowed.js +27 -0
  15. package/dist/cjs/utils/is-supported-node.js +15 -0
  16. package/dist/es2019/commands/remove-breakout.js +22 -0
  17. package/dist/es2019/commands/set-breakout-mode.js +23 -0
  18. package/dist/es2019/index.js +1 -0
  19. package/dist/es2019/plugin-key.js +3 -0
  20. package/dist/es2019/plugin.js +208 -0
  21. package/dist/es2019/types.js +1 -0
  22. package/dist/es2019/ui/LayoutButton.js +111 -0
  23. package/dist/es2019/utils/find-breakout-node.js +37 -0
  24. package/dist/es2019/utils/get-breakout-mode.js +17 -0
  25. package/dist/es2019/utils/is-breakout-mark-allowed.js +22 -0
  26. package/dist/es2019/utils/is-supported-node.js +9 -0
  27. package/dist/esm/commands/remove-breakout.js +24 -0
  28. package/dist/esm/commands/set-breakout-mode.js +23 -0
  29. package/dist/esm/index.js +1 -0
  30. package/dist/esm/plugin-key.js +5 -0
  31. package/dist/esm/plugin.js +211 -0
  32. package/dist/esm/types.js +1 -0
  33. package/dist/esm/ui/LayoutButton.js +106 -0
  34. package/dist/esm/utils/find-breakout-node.js +37 -0
  35. package/dist/esm/utils/get-breakout-mode.js +19 -0
  36. package/dist/esm/utils/is-breakout-mark-allowed.js +22 -0
  37. package/dist/esm/utils/is-supported-node.js +9 -0
  38. package/dist/types/commands/remove-breakout.d.ts +2 -0
  39. package/dist/types/commands/set-breakout-mode.d.ts +2 -0
  40. package/dist/types/index.d.ts +2 -0
  41. package/dist/types/plugin-key.d.ts +5 -0
  42. package/dist/types/plugin.d.ts +13 -0
  43. package/dist/types/types.d.ts +4 -0
  44. package/dist/types/ui/LayoutButton.d.ts +16 -0
  45. package/dist/types/utils/find-breakout-node.d.ts +9 -0
  46. package/dist/types/utils/get-breakout-mode.d.ts +7 -0
  47. package/dist/types/utils/is-breakout-mark-allowed.d.ts +9 -0
  48. package/dist/types/utils/is-supported-node.d.ts +6 -0
  49. package/dist/types-ts4.5/commands/remove-breakout.d.ts +2 -0
  50. package/dist/types-ts4.5/commands/set-breakout-mode.d.ts +2 -0
  51. package/dist/types-ts4.5/index.d.ts +2 -0
  52. package/dist/types-ts4.5/plugin-key.d.ts +5 -0
  53. package/dist/types-ts4.5/plugin.d.ts +15 -0
  54. package/dist/types-ts4.5/types.d.ts +4 -0
  55. package/dist/types-ts4.5/ui/LayoutButton.d.ts +16 -0
  56. package/dist/types-ts4.5/utils/find-breakout-node.d.ts +9 -0
  57. package/dist/types-ts4.5/utils/get-breakout-mode.d.ts +7 -0
  58. package/dist/types-ts4.5/utils/is-breakout-mark-allowed.d.ts +9 -0
  59. package/dist/types-ts4.5/utils/is-supported-node.d.ts +6 -0
  60. package/package.json +100 -0
@@ -0,0 +1,208 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import React from 'react';
3
+ import { breakout } from '@atlaskit/adf-schema';
4
+ import { useSharedPluginState } from '@atlaskit/editor-common/hooks';
5
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
6
+ import { BreakoutCssClassName } from '@atlaskit/editor-common/styles';
7
+ import { calcBreakoutWidthPx } from '@atlaskit/editor-common/utils';
8
+ import { akEditorSwoopCubicBezier } from '@atlaskit/editor-shared-styles';
9
+ import { pluginKey } from './plugin-key';
10
+ import LayoutButton from './ui/LayoutButton';
11
+ import { findSupportedNodeForBreakout } from './utils/find-breakout-node';
12
+ class BreakoutView {
13
+ constructor(
14
+ /**
15
+ * Note: this is actually a PMMark -- however our version
16
+ * of the prosemirror and prosemirror types mean using PMNode
17
+ * is not problematic.
18
+ */
19
+ mark, view, pluginInjectionApi) {
20
+ _defineProperty(this, "updateWidth", widthState => {
21
+ // we skip updating the width of breakout nodes if the editorView dom
22
+ // element was hidden (to avoid breakout width and button thrashing
23
+ // when an editor is hidden, re-rendered and unhidden).
24
+ if (widthState === undefined || widthState.width === 0) {
25
+ return;
26
+ }
27
+ let containerStyle = ``;
28
+ let contentStyle = ``;
29
+ let breakoutWidthPx = calcBreakoutWidthPx(this.mark.attrs.mode, widthState.width);
30
+ if (widthState.lineLength) {
31
+ if (breakoutWidthPx < widthState.lineLength) {
32
+ breakoutWidthPx = widthState.lineLength;
33
+ }
34
+ containerStyle += `
35
+ transform: none;
36
+ display: flex;
37
+ justify-content: center;
38
+ `;
39
+
40
+ // There is a delay in the animation because widthState is delayed.
41
+ // When the editor goes full width the animation for the editor
42
+ // begins and finishes before widthState can update the new dimensions.
43
+ contentStyle += `
44
+ min-width: ${breakoutWidthPx}px;
45
+ transition: min-width 0.5s ${akEditorSwoopCubicBezier};
46
+ `;
47
+ } else {
48
+ // fallback method
49
+ // (lineLength is not normally undefined, but might be in e.g. SSR or initial render)
50
+ //
51
+ // this approach doesn't work well with position: fixed, so
52
+ // it breaks things like sticky headers
53
+ containerStyle += `width: ${breakoutWidthPx}px; transform: translateX(-50%); margin-left: 50%;`;
54
+ }
55
+
56
+ // NOTE: This is a hack to ignore mutation since mark NodeView doesn't support
57
+ // `ignoreMutation` life-cycle event. @see ED-9947
58
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
+ const viewDomObserver = this.view.domObserver;
60
+ if (viewDomObserver && this.view.dom) {
61
+ viewDomObserver.stop();
62
+ setTimeout(() => {
63
+ viewDomObserver.start();
64
+ }, 0);
65
+ }
66
+ if (typeof this.dom.style.cssText !== 'undefined') {
67
+ this.dom.style.cssText = containerStyle;
68
+ this.contentDOM.style.cssText = contentStyle;
69
+ } else {
70
+ this.dom.setAttribute('style', containerStyle);
71
+ this.contentDOM.setAttribute('style', contentStyle);
72
+ }
73
+ });
74
+ const contentDOM = document.createElement('div');
75
+ contentDOM.className = BreakoutCssClassName.BREAKOUT_MARK_DOM;
76
+ const dom = document.createElement('div');
77
+ dom.className = BreakoutCssClassName.BREAKOUT_MARK;
78
+ dom.setAttribute('data-layout', mark.attrs.mode);
79
+ dom.appendChild(contentDOM);
80
+ this.dom = dom;
81
+ this.mark = mark;
82
+ this.view = view;
83
+ this.contentDOM = contentDOM;
84
+ this.unsubscribe = pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : pluginInjectionApi.width.sharedState.onChange(({
85
+ nextSharedState
86
+ }) => this.updateWidth(nextSharedState));
87
+ this.updateWidth(pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : pluginInjectionApi.width.sharedState.currentState());
88
+ }
89
+ // NOTE: Lifecycle events doesn't work for mark NodeView. So currently this is a no-op.
90
+ // @see https://github.com/ProseMirror/prosemirror/issues/1082
91
+ destroy() {
92
+ var _this$unsubscribe;
93
+ (_this$unsubscribe = this.unsubscribe) === null || _this$unsubscribe === void 0 ? void 0 : _this$unsubscribe.call(this);
94
+ }
95
+ }
96
+ function shouldPluginStateUpdate(newBreakoutNode, currentBreakoutNode) {
97
+ if (newBreakoutNode && currentBreakoutNode) {
98
+ return newBreakoutNode !== currentBreakoutNode;
99
+ }
100
+ return newBreakoutNode || currentBreakoutNode ? true : false;
101
+ }
102
+ function createPlugin(pluginInjectionApi, {
103
+ dispatch
104
+ }) {
105
+ return new SafePlugin({
106
+ state: {
107
+ init() {
108
+ return {
109
+ breakoutNode: undefined
110
+ };
111
+ },
112
+ apply(tr, pluginState) {
113
+ const breakoutNode = findSupportedNodeForBreakout(tr.selection);
114
+ if (shouldPluginStateUpdate(breakoutNode, pluginState.breakoutNode)) {
115
+ const nextPluginState = {
116
+ ...pluginState,
117
+ breakoutNode
118
+ };
119
+ dispatch(pluginKey, nextPluginState);
120
+ return nextPluginState;
121
+ }
122
+ return pluginState;
123
+ }
124
+ },
125
+ key: pluginKey,
126
+ props: {
127
+ nodeViews: {
128
+ // Note: When we upgrade to prosemirror 1.27.2 -- we should
129
+ // move this to markViews.
130
+ // See the following link for more details:
131
+ // https://prosemirror.net/docs/ref/#view.EditorProps.nodeViews.
132
+ breakout: (mark, view) => {
133
+ return new BreakoutView(mark, view, pluginInjectionApi);
134
+ }
135
+ }
136
+ }
137
+ });
138
+ }
139
+ const LayoutButtonWrapper = ({
140
+ api,
141
+ editorView,
142
+ boundariesElement,
143
+ scrollableElement,
144
+ mountPoint
145
+ }) => {
146
+ var _breakoutState$breako, _breakoutState$breako2;
147
+ // Re-render with `width` (but don't use state) due to https://bitbucket.org/atlassian/%7Bc8e2f021-38d2-46d0-9b7a-b3f7b428f724%7D/pull-requests/24272
148
+ const {
149
+ breakoutState
150
+ } = useSharedPluginState(api, ['width', 'breakout']);
151
+ return /*#__PURE__*/React.createElement(LayoutButton, {
152
+ editorView: editorView,
153
+ mountPoint: mountPoint,
154
+ boundariesElement: boundariesElement,
155
+ scrollableElement: scrollableElement,
156
+ node: (_breakoutState$breako = breakoutState === null || breakoutState === void 0 ? void 0 : (_breakoutState$breako2 = breakoutState.breakoutNode) === null || _breakoutState$breako2 === void 0 ? void 0 : _breakoutState$breako2.node) !== null && _breakoutState$breako !== void 0 ? _breakoutState$breako : null
157
+ });
158
+ };
159
+ export const breakoutPlugin = ({
160
+ config: options,
161
+ api
162
+ }) => ({
163
+ name: 'breakout',
164
+ pmPlugins() {
165
+ return [{
166
+ name: 'breakout',
167
+ plugin: props => createPlugin(api, props)
168
+ }];
169
+ },
170
+ marks() {
171
+ return [{
172
+ name: 'breakout',
173
+ mark: breakout
174
+ }];
175
+ },
176
+ getSharedState(editorState) {
177
+ if (!editorState) {
178
+ return {
179
+ breakoutNode: undefined
180
+ };
181
+ }
182
+ const pluginState = pluginKey.getState(editorState);
183
+ if (!pluginState) {
184
+ return {
185
+ breakoutNode: undefined
186
+ };
187
+ }
188
+ return pluginState;
189
+ },
190
+ contentComponent({
191
+ editorView,
192
+ popupsMountPoint,
193
+ popupsBoundariesElement,
194
+ popupsScrollableElement
195
+ }) {
196
+ // This is a bit crappy, but should be resolved once we move to a static schema.
197
+ if (options && !options.allowBreakoutButton) {
198
+ return null;
199
+ }
200
+ return /*#__PURE__*/React.createElement(LayoutButtonWrapper, {
201
+ api: api,
202
+ mountPoint: popupsMountPoint,
203
+ editorView: editorView,
204
+ boundariesElement: popupsBoundariesElement,
205
+ scrollableElement: popupsScrollableElement
206
+ });
207
+ }
208
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,111 @@
1
+ /** @jsx jsx */
2
+ import { useCallback } from 'react';
3
+ import { css, jsx } from '@emotion/react';
4
+ import { injectIntl } from 'react-intl-next';
5
+ import { BreakoutCssClassName } from '@atlaskit/editor-common/styles';
6
+ import { Popup } from '@atlaskit/editor-common/ui';
7
+ import { ToolbarButton } from '@atlaskit/editor-common/ui-menu';
8
+ import { getNextBreakoutMode, getTitle } from '@atlaskit/editor-common/utils';
9
+ import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
10
+ import { findDomRefAtPos, findParentDomRefOfType } from '@atlaskit/editor-prosemirror/utils';
11
+ import CollapseIcon from '@atlaskit/icon/glyph/editor/collapse';
12
+ import ExpandIcon from '@atlaskit/icon/glyph/editor/expand';
13
+ import { B300, N20A, N300 } from '@atlaskit/theme/colors';
14
+ import { layers } from '@atlaskit/theme/constants';
15
+ import { removeBreakout } from '../commands/remove-breakout';
16
+ import { setBreakoutMode } from '../commands/set-breakout-mode';
17
+ import { getPluginState } from '../plugin-key';
18
+ import { getBreakoutMode } from '../utils/get-breakout-mode';
19
+ import { isBreakoutMarkAllowed } from '../utils/is-breakout-mark-allowed';
20
+ import { isSupportedNodeForBreakout } from '../utils/is-supported-node';
21
+ const toolbarButtonWrapperStyles = css({
22
+ // eslint-disable-next-line @atlaskit/design-system/no-nested-styles
23
+ '&& button': {
24
+ background: `var(--ds-background-neutral, ${N20A})`,
25
+ color: `var(--ds-icon, ${N300})`,
26
+ ':hover': {
27
+ background: `var(--ds-background-neutral-hovered, ${B300})`,
28
+ color: `${"var(--ds-icon, white)"} !important`
29
+ }
30
+ }
31
+ });
32
+ function getBreakoutNodeElement(pluginState, selection, editorView) {
33
+ if (!pluginState.breakoutNode) {
34
+ return undefined;
35
+ }
36
+ if (selection instanceof NodeSelection && isSupportedNodeForBreakout(selection.node)) {
37
+ return findDomRefAtPos(selection.from, editorView.domAtPos.bind(editorView));
38
+ }
39
+ return findParentDomRefOfType(pluginState.breakoutNode.node.type, editorView.domAtPos.bind(editorView))(selection);
40
+ }
41
+ const LayoutButton = ({
42
+ intl: {
43
+ formatMessage
44
+ },
45
+ mountPoint,
46
+ boundariesElement,
47
+ scrollableElement,
48
+ editorView,
49
+ node
50
+ }) => {
51
+ const handleClick = useCallback(breakoutMode => {
52
+ const {
53
+ state,
54
+ dispatch
55
+ } = editorView;
56
+ if (['wide', 'full-width'].indexOf(breakoutMode) !== -1) {
57
+ setBreakoutMode(breakoutMode)(state, dispatch);
58
+ } else {
59
+ removeBreakout()(state, dispatch);
60
+ }
61
+ }, [editorView]);
62
+ const {
63
+ state
64
+ } = editorView;
65
+ if (!node || !isBreakoutMarkAllowed(state)) {
66
+ return null;
67
+ }
68
+ const breakoutMode = getBreakoutMode(editorView.state);
69
+ const titleMessage = getTitle(breakoutMode);
70
+ const title = formatMessage(titleMessage);
71
+ const nextBreakoutMode = getNextBreakoutMode(breakoutMode);
72
+ const belowOtherPopupsZIndex = layers.layer() - 1;
73
+ let pluginState = getPluginState(state);
74
+ if (!pluginState) {
75
+ return null;
76
+ }
77
+ let element = getBreakoutNodeElement(pluginState, state.selection, editorView);
78
+ if (!element) {
79
+ return null;
80
+ }
81
+ const closestEl = element.querySelector(`.${BreakoutCssClassName.BREAKOUT_MARK_DOM}`);
82
+ if (closestEl && closestEl.firstChild) {
83
+ element = closestEl.firstChild;
84
+ }
85
+ return jsx(Popup, {
86
+ ariaLabel: title,
87
+ target: element,
88
+ offset: [5, 0],
89
+ alignY: "start",
90
+ alignX: "end",
91
+ mountTo: mountPoint,
92
+ boundariesElement: boundariesElement,
93
+ scrollableElement: scrollableElement,
94
+ stick: true,
95
+ forcePlacement: true,
96
+ zIndex: belowOtherPopupsZIndex
97
+ }, jsx("div", {
98
+ css: toolbarButtonWrapperStyles
99
+ }, jsx(ToolbarButton, {
100
+ title: title,
101
+ testId: titleMessage.id,
102
+ onClick: () => handleClick(nextBreakoutMode),
103
+ iconBefore: breakoutMode === 'full-width' ? jsx(CollapseIcon, {
104
+ label: title
105
+ }) : jsx(ExpandIcon, {
106
+ label: title
107
+ })
108
+ })));
109
+ };
110
+ LayoutButton.displayName = 'LayoutButton';
111
+ export default injectIntl(LayoutButton);
@@ -0,0 +1,37 @@
1
+ import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
2
+ import { findParentNode } from '@atlaskit/editor-prosemirror/utils';
3
+ import { isSupportedNodeForBreakout } from './is-supported-node';
4
+
5
+ /**
6
+ * Find the nearest parent node to the selection that supports breakout, or if the nearest
7
+ * matching parent node is the doc, return undefined.
8
+ * For depth, if a node is selected and supports breakout, return the depth of the node.
9
+ * @param selection Current editor selection
10
+ */
11
+ export function findSupportedNodeForBreakout(selection) {
12
+ if (selection instanceof NodeSelection) {
13
+ const supportsBreakout = isSupportedNodeForBreakout(selection.node);
14
+ if (supportsBreakout) {
15
+ return {
16
+ pos: selection.from,
17
+ start: selection.from,
18
+ node: selection.node,
19
+ // If a selected expand is in a doc, the depth of that expand is 0. Therefore
20
+ // we don't need to subtract 1 or instantly return false if the depth is 0
21
+ depth: selection.$anchor.depth
22
+ };
23
+ }
24
+ }
25
+ const breakoutNode = findParentNode(isSupportedNodeForBreakout)(selection);
26
+ if (!breakoutNode || breakoutNode.depth === 0) {
27
+ // If this node doesn't exist or the only supporting node is the document
28
+ // (with depth 0), then we're not inside a node that supports breakout
29
+ return undefined;
30
+ }
31
+ return {
32
+ node: breakoutNode.node,
33
+ start: breakoutNode.start,
34
+ pos: breakoutNode.pos,
35
+ depth: breakoutNode.depth - 1
36
+ };
37
+ }
@@ -0,0 +1,17 @@
1
+ import { findSupportedNodeForBreakout } from './find-breakout-node';
2
+
3
+ /**
4
+ * Get the current mode of the breakout at the selection
5
+ * @param state Current EditorState
6
+ */
7
+ export function getBreakoutMode(state) {
8
+ const node = findSupportedNodeForBreakout(state.selection);
9
+ if (!node) {
10
+ return;
11
+ }
12
+ const breakoutMark = node.node.marks.find(m => m.type.name === 'breakout');
13
+ if (!breakoutMark) {
14
+ return;
15
+ }
16
+ return breakoutMark.attrs.mode;
17
+ }
@@ -0,0 +1,22 @@
1
+ import { findSupportedNodeForBreakout } from './find-breakout-node';
2
+
3
+ /**
4
+ * Check if breakout should be allowed for the current selection. If a node is selected,
5
+ * can this node be broken out, if text, can the enclosing parent node be broken out.
6
+ *
7
+ * Currently breakout of a node is not possible if it's nested in anything but the document, however
8
+ * this logic supports this changing.
9
+ */
10
+ export function isBreakoutMarkAllowed(state) {
11
+ if (!state.schema.marks.breakout) {
12
+ return false;
13
+ }
14
+ const supportedNodeParent = findSupportedNodeForBreakout(state.selection);
15
+ if (!supportedNodeParent) {
16
+ return false;
17
+ }
18
+ // At the moment we can only breakout when the depth is 0, ie. doc is the only node
19
+ // that supports breakout. This *could* change though.
20
+ const parent = state.selection.$from.node(supportedNodeParent.depth);
21
+ return parent.type.allowsMarkType(state.schema.marks.breakout);
22
+ }
@@ -0,0 +1,9 @@
1
+ const supportedNodesForBreakout = ['codeBlock', 'layoutSection', 'expand'];
2
+
3
+ /**
4
+ * Check if breakout can be applied to a node
5
+ * @param node Node to check
6
+ */
7
+ export function isSupportedNodeForBreakout(node) {
8
+ return supportedNodesForBreakout.indexOf(node.type.name) !== -1;
9
+ }
@@ -0,0 +1,24 @@
1
+ import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
2
+ import { findSupportedNodeForBreakout } from '../utils/find-breakout-node';
3
+ export function removeBreakout() {
4
+ return function (state, dispatch) {
5
+ var node = findSupportedNodeForBreakout(state.selection);
6
+ if (!node) {
7
+ return false;
8
+ }
9
+ var marks = node.node.marks.filter(function (m) {
10
+ return m.type.name !== 'breakout';
11
+ });
12
+ var tr = state.tr.setNodeMarkup(node.pos, node.node.type, node.node.attrs, marks);
13
+ tr.setMeta('scrollIntoView', false);
14
+ if (state.selection instanceof NodeSelection) {
15
+ if (state.selection.$anchor.pos === node.pos) {
16
+ tr.setSelection(NodeSelection.create(tr.doc, node.pos));
17
+ }
18
+ }
19
+ if (dispatch) {
20
+ dispatch(tr);
21
+ }
22
+ return true;
23
+ };
24
+ }
@@ -0,0 +1,23 @@
1
+ import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
2
+ import { findSupportedNodeForBreakout } from '../utils/find-breakout-node';
3
+ export function setBreakoutMode(mode) {
4
+ return function (state, dispatch) {
5
+ var node = findSupportedNodeForBreakout(state.selection);
6
+ if (!node) {
7
+ return false;
8
+ }
9
+ var tr = state.tr.setNodeMarkup(node.pos, node.node.type, node.node.attrs, [state.schema.marks.breakout.create({
10
+ mode: mode
11
+ })]);
12
+ tr.setMeta('scrollIntoView', false);
13
+ if (state.selection instanceof NodeSelection) {
14
+ if (state.selection.$anchor.pos === node.pos) {
15
+ tr.setSelection(NodeSelection.create(tr.doc, node.pos));
16
+ }
17
+ }
18
+ if (dispatch) {
19
+ dispatch(tr);
20
+ }
21
+ return true;
22
+ };
23
+ }
@@ -0,0 +1 @@
1
+ export { breakoutPlugin } from './plugin';
@@ -0,0 +1,5 @@
1
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
2
+ export var pluginKey = new PluginKey('breakoutPlugin');
3
+ export var getPluginState = function getPluginState(state) {
4
+ return pluginKey.getState(state) || undefined;
5
+ };
@@ -0,0 +1,211 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
+ 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; }
5
+ 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; }
6
+ import React from 'react';
7
+ import { breakout } from '@atlaskit/adf-schema';
8
+ import { useSharedPluginState } from '@atlaskit/editor-common/hooks';
9
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
10
+ import { BreakoutCssClassName } from '@atlaskit/editor-common/styles';
11
+ import { calcBreakoutWidthPx } from '@atlaskit/editor-common/utils';
12
+ import { akEditorSwoopCubicBezier } from '@atlaskit/editor-shared-styles';
13
+ import { pluginKey } from './plugin-key';
14
+ import LayoutButton from './ui/LayoutButton';
15
+ import { findSupportedNodeForBreakout } from './utils/find-breakout-node';
16
+ var BreakoutView = /*#__PURE__*/function () {
17
+ function BreakoutView(
18
+ /**
19
+ * Note: this is actually a PMMark -- however our version
20
+ * of the prosemirror and prosemirror types mean using PMNode
21
+ * is not problematic.
22
+ */
23
+ mark, view, pluginInjectionApi) {
24
+ var _this = this;
25
+ _classCallCheck(this, BreakoutView);
26
+ _defineProperty(this, "updateWidth", function (widthState) {
27
+ // we skip updating the width of breakout nodes if the editorView dom
28
+ // element was hidden (to avoid breakout width and button thrashing
29
+ // when an editor is hidden, re-rendered and unhidden).
30
+ if (widthState === undefined || widthState.width === 0) {
31
+ return;
32
+ }
33
+ var containerStyle = "";
34
+ var contentStyle = "";
35
+ var breakoutWidthPx = calcBreakoutWidthPx(_this.mark.attrs.mode, widthState.width);
36
+ if (widthState.lineLength) {
37
+ if (breakoutWidthPx < widthState.lineLength) {
38
+ breakoutWidthPx = widthState.lineLength;
39
+ }
40
+ containerStyle += "\n transform: none;\n display: flex;\n justify-content: center;\n ";
41
+
42
+ // There is a delay in the animation because widthState is delayed.
43
+ // When the editor goes full width the animation for the editor
44
+ // begins and finishes before widthState can update the new dimensions.
45
+ contentStyle += "\n min-width: ".concat(breakoutWidthPx, "px;\n transition: min-width 0.5s ").concat(akEditorSwoopCubicBezier, ";\n ");
46
+ } else {
47
+ // fallback method
48
+ // (lineLength is not normally undefined, but might be in e.g. SSR or initial render)
49
+ //
50
+ // this approach doesn't work well with position: fixed, so
51
+ // it breaks things like sticky headers
52
+ containerStyle += "width: ".concat(breakoutWidthPx, "px; transform: translateX(-50%); margin-left: 50%;");
53
+ }
54
+
55
+ // NOTE: This is a hack to ignore mutation since mark NodeView doesn't support
56
+ // `ignoreMutation` life-cycle event. @see ED-9947
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
+ var viewDomObserver = _this.view.domObserver;
59
+ if (viewDomObserver && _this.view.dom) {
60
+ viewDomObserver.stop();
61
+ setTimeout(function () {
62
+ viewDomObserver.start();
63
+ }, 0);
64
+ }
65
+ if (typeof _this.dom.style.cssText !== 'undefined') {
66
+ _this.dom.style.cssText = containerStyle;
67
+ _this.contentDOM.style.cssText = contentStyle;
68
+ } else {
69
+ _this.dom.setAttribute('style', containerStyle);
70
+ _this.contentDOM.setAttribute('style', contentStyle);
71
+ }
72
+ });
73
+ var contentDOM = document.createElement('div');
74
+ contentDOM.className = BreakoutCssClassName.BREAKOUT_MARK_DOM;
75
+ var dom = document.createElement('div');
76
+ dom.className = BreakoutCssClassName.BREAKOUT_MARK;
77
+ dom.setAttribute('data-layout', mark.attrs.mode);
78
+ dom.appendChild(contentDOM);
79
+ this.dom = dom;
80
+ this.mark = mark;
81
+ this.view = view;
82
+ this.contentDOM = contentDOM;
83
+ this.unsubscribe = pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : pluginInjectionApi.width.sharedState.onChange(function (_ref) {
84
+ var nextSharedState = _ref.nextSharedState;
85
+ return _this.updateWidth(nextSharedState);
86
+ });
87
+ this.updateWidth(pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : pluginInjectionApi.width.sharedState.currentState());
88
+ }
89
+ _createClass(BreakoutView, [{
90
+ key: "destroy",
91
+ value:
92
+ // NOTE: Lifecycle events doesn't work for mark NodeView. So currently this is a no-op.
93
+ // @see https://github.com/ProseMirror/prosemirror/issues/1082
94
+ function destroy() {
95
+ var _this$unsubscribe;
96
+ (_this$unsubscribe = this.unsubscribe) === null || _this$unsubscribe === void 0 || _this$unsubscribe.call(this);
97
+ }
98
+ }]);
99
+ return BreakoutView;
100
+ }();
101
+ function shouldPluginStateUpdate(newBreakoutNode, currentBreakoutNode) {
102
+ if (newBreakoutNode && currentBreakoutNode) {
103
+ return newBreakoutNode !== currentBreakoutNode;
104
+ }
105
+ return newBreakoutNode || currentBreakoutNode ? true : false;
106
+ }
107
+ function createPlugin(pluginInjectionApi, _ref2) {
108
+ var dispatch = _ref2.dispatch;
109
+ return new SafePlugin({
110
+ state: {
111
+ init: function init() {
112
+ return {
113
+ breakoutNode: undefined
114
+ };
115
+ },
116
+ apply: function apply(tr, pluginState) {
117
+ var breakoutNode = findSupportedNodeForBreakout(tr.selection);
118
+ if (shouldPluginStateUpdate(breakoutNode, pluginState.breakoutNode)) {
119
+ var nextPluginState = _objectSpread(_objectSpread({}, pluginState), {}, {
120
+ breakoutNode: breakoutNode
121
+ });
122
+ dispatch(pluginKey, nextPluginState);
123
+ return nextPluginState;
124
+ }
125
+ return pluginState;
126
+ }
127
+ },
128
+ key: pluginKey,
129
+ props: {
130
+ nodeViews: {
131
+ // Note: When we upgrade to prosemirror 1.27.2 -- we should
132
+ // move this to markViews.
133
+ // See the following link for more details:
134
+ // https://prosemirror.net/docs/ref/#view.EditorProps.nodeViews.
135
+ breakout: function breakout(mark, view) {
136
+ return new BreakoutView(mark, view, pluginInjectionApi);
137
+ }
138
+ }
139
+ }
140
+ });
141
+ }
142
+ var LayoutButtonWrapper = function LayoutButtonWrapper(_ref3) {
143
+ var _breakoutState$breako, _breakoutState$breako2;
144
+ var api = _ref3.api,
145
+ editorView = _ref3.editorView,
146
+ boundariesElement = _ref3.boundariesElement,
147
+ scrollableElement = _ref3.scrollableElement,
148
+ mountPoint = _ref3.mountPoint;
149
+ // Re-render with `width` (but don't use state) due to https://bitbucket.org/atlassian/%7Bc8e2f021-38d2-46d0-9b7a-b3f7b428f724%7D/pull-requests/24272
150
+ var _useSharedPluginState = useSharedPluginState(api, ['width', 'breakout']),
151
+ breakoutState = _useSharedPluginState.breakoutState;
152
+ return /*#__PURE__*/React.createElement(LayoutButton, {
153
+ editorView: editorView,
154
+ mountPoint: mountPoint,
155
+ boundariesElement: boundariesElement,
156
+ scrollableElement: scrollableElement,
157
+ node: (_breakoutState$breako = breakoutState === null || breakoutState === void 0 || (_breakoutState$breako2 = breakoutState.breakoutNode) === null || _breakoutState$breako2 === void 0 ? void 0 : _breakoutState$breako2.node) !== null && _breakoutState$breako !== void 0 ? _breakoutState$breako : null
158
+ });
159
+ };
160
+ export var breakoutPlugin = function breakoutPlugin(_ref4) {
161
+ var options = _ref4.config,
162
+ api = _ref4.api;
163
+ return {
164
+ name: 'breakout',
165
+ pmPlugins: function pmPlugins() {
166
+ return [{
167
+ name: 'breakout',
168
+ plugin: function plugin(props) {
169
+ return createPlugin(api, props);
170
+ }
171
+ }];
172
+ },
173
+ marks: function marks() {
174
+ return [{
175
+ name: 'breakout',
176
+ mark: breakout
177
+ }];
178
+ },
179
+ getSharedState: function getSharedState(editorState) {
180
+ if (!editorState) {
181
+ return {
182
+ breakoutNode: undefined
183
+ };
184
+ }
185
+ var pluginState = pluginKey.getState(editorState);
186
+ if (!pluginState) {
187
+ return {
188
+ breakoutNode: undefined
189
+ };
190
+ }
191
+ return pluginState;
192
+ },
193
+ contentComponent: function contentComponent(_ref5) {
194
+ var editorView = _ref5.editorView,
195
+ popupsMountPoint = _ref5.popupsMountPoint,
196
+ popupsBoundariesElement = _ref5.popupsBoundariesElement,
197
+ popupsScrollableElement = _ref5.popupsScrollableElement;
198
+ // This is a bit crappy, but should be resolved once we move to a static schema.
199
+ if (options && !options.allowBreakoutButton) {
200
+ return null;
201
+ }
202
+ return /*#__PURE__*/React.createElement(LayoutButtonWrapper, {
203
+ api: api,
204
+ mountPoint: popupsMountPoint,
205
+ editorView: editorView,
206
+ boundariesElement: popupsBoundariesElement,
207
+ scrollableElement: popupsScrollableElement
208
+ });
209
+ }
210
+ };
211
+ };
@@ -0,0 +1 @@
1
+ export {};