@atlaskit/editor-plugin-layout 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 (46) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE.md +13 -0
  3. package/README.md +30 -0
  4. package/dist/cjs/actions.js +345 -0
  5. package/dist/cjs/index.js +12 -0
  6. package/dist/cjs/plugin.js +91 -0
  7. package/dist/cjs/pm-plugins/main.js +148 -0
  8. package/dist/cjs/pm-plugins/plugin-key.js +8 -0
  9. package/dist/cjs/pm-plugins/types.js +5 -0
  10. package/dist/cjs/toolbar.js +118 -0
  11. package/dist/cjs/types.js +5 -0
  12. package/dist/es2019/actions.js +328 -0
  13. package/dist/es2019/index.js +1 -0
  14. package/dist/es2019/plugin.js +75 -0
  15. package/dist/es2019/pm-plugins/main.js +142 -0
  16. package/dist/es2019/pm-plugins/plugin-key.js +2 -0
  17. package/dist/es2019/pm-plugins/types.js +1 -0
  18. package/dist/es2019/toolbar.js +100 -0
  19. package/dist/es2019/types.js +1 -0
  20. package/dist/esm/actions.js +336 -0
  21. package/dist/esm/index.js +1 -0
  22. package/dist/esm/plugin.js +79 -0
  23. package/dist/esm/pm-plugins/main.js +141 -0
  24. package/dist/esm/pm-plugins/plugin-key.js +2 -0
  25. package/dist/esm/pm-plugins/types.js +1 -0
  26. package/dist/esm/toolbar.js +108 -0
  27. package/dist/esm/types.js +1 -0
  28. package/dist/types/actions.d.ts +22 -0
  29. package/dist/types/index.d.ts +3 -0
  30. package/dist/types/plugin.d.ts +15 -0
  31. package/dist/types/pm-plugins/main.d.ts +6 -0
  32. package/dist/types/pm-plugins/plugin-key.d.ts +3 -0
  33. package/dist/types/pm-plugins/types.d.ts +14 -0
  34. package/dist/types/toolbar.d.ts +6 -0
  35. package/dist/types/types.d.ts +13 -0
  36. package/dist/types-ts4.5/actions.d.ts +22 -0
  37. package/dist/types-ts4.5/index.d.ts +3 -0
  38. package/dist/types-ts4.5/plugin.d.ts +18 -0
  39. package/dist/types-ts4.5/pm-plugins/main.d.ts +6 -0
  40. package/dist/types-ts4.5/pm-plugins/plugin-key.d.ts +3 -0
  41. package/dist/types-ts4.5/pm-plugins/types.d.ts +14 -0
  42. package/dist/types-ts4.5/toolbar.d.ts +6 -0
  43. package/dist/types-ts4.5/types.d.ts +13 -0
  44. package/package.json +94 -0
  45. package/report.api.md +73 -0
  46. package/tmp/api-report-tmp.d.ts +43 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ # @atlaskit/editor-plugin-layout
package/LICENSE.md ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2023 Atlassian Pty Ltd
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Editor plugin layout
2
+
3
+ Layout plugin for @atlaskit/editor-core
4
+
5
+ **Note:** This component is designed for internal Atlassian development.
6
+ External contributors will be able to use this component but will not be able to submit issues.
7
+
8
+ ## Install
9
+ ---
10
+ - **Install** - *yarn add @atlaskit/editor-plugin-layout*
11
+ - **npm** - [@atlaskit/editor-plugin-layout](https://www.npmjs.com/package/@atlaskit/editor-plugin-layout)
12
+ - **Source** - [Bitbucket](https://bitbucket.org/atlassian/atlassian-frontend/src/master/packages/editor/editor-plugin-layout)
13
+ - **Bundle** - [unpkg.com](https://unpkg.com/@atlaskit/editor-plugin-layout/dist/)
14
+
15
+ ## Usage
16
+ ---
17
+ **Internal use only**
18
+
19
+ @atlaskit/editor-plugin-layout is intended for internal use by the @atlaskit/editor-core and as a plugin dependency of the Editor within your product.
20
+
21
+ Direct use of this component is not supported.
22
+
23
+ Please see [Atlaskit - Editor plugin layout](https://atlaskit.atlassian.com/packages/editor/editor-plugin-layout) for documentation and examples for this package.
24
+
25
+ ## Support
26
+ ---
27
+ For internal Atlassian, visit the slack channel [#help-editor](https://atlassian.slack.com/archives/CFG3PSQ9E) for support or visit [go/editor-help](https://go/editor-help) to submit a bug.
28
+ ## License
29
+ ---
30
+ Please see [Atlassian Frontend - License](https://hello.atlassian.net/wiki/spaces/AF/pages/2589099144/Documentation#License) for more licensing information.
@@ -0,0 +1,345 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.fixColumnStructure = exports.fixColumnSizes = exports.deleteActiveLayoutNode = exports.createDefaultLayoutSection = exports.TWO_COL_LAYOUTS = exports.THREE_COL_LAYOUTS = exports.ONE_COL_LAYOUTS = void 0;
8
+ exports.forceSectionToPresetLayout = forceSectionToPresetLayout;
9
+ exports.setPresetLayout = exports.insertLayoutColumnsWithAnalytics = exports.insertLayoutColumns = exports.getSelectedLayout = exports.getPresetLayout = void 0;
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+ var _analytics = require("@atlaskit/editor-common/analytics");
12
+ var _editorAnalytics = require("@atlaskit/editor-common/editor-analytics");
13
+ var _utils = require("@atlaskit/editor-common/utils");
14
+ var _model = require("@atlaskit/editor-prosemirror/model");
15
+ var _state = require("@atlaskit/editor-prosemirror/state");
16
+ var _utils2 = require("@atlaskit/editor-prosemirror/utils");
17
+ var _pluginKey = require("./pm-plugins/plugin-key");
18
+ 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; }
19
+ 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) { (0, _defineProperty2.default)(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; }
20
+ var ONE_COL_LAYOUTS = exports.ONE_COL_LAYOUTS = ['single'];
21
+ var TWO_COL_LAYOUTS = exports.TWO_COL_LAYOUTS = ['two_equal', 'two_left_sidebar', 'two_right_sidebar'];
22
+ var THREE_COL_LAYOUTS = exports.THREE_COL_LAYOUTS = ['three_equal', 'three_with_sidebars'];
23
+ var getWidthsForPreset = function getWidthsForPreset(presetLayout) {
24
+ switch (presetLayout) {
25
+ case 'single':
26
+ return [100];
27
+ case 'two_equal':
28
+ return [50, 50];
29
+ case 'three_equal':
30
+ return [33.33, 33.33, 33.33];
31
+ case 'two_left_sidebar':
32
+ return [33.33, 66.66];
33
+ case 'two_right_sidebar':
34
+ return [66.66, 33.33];
35
+ case 'three_with_sidebars':
36
+ return [25, 50, 25];
37
+ }
38
+ };
39
+
40
+ /**
41
+ * Finds layout preset based on the width attrs of all the layoutColumn nodes
42
+ * inside the layoutSection node
43
+ */
44
+ var getPresetLayout = exports.getPresetLayout = function getPresetLayout(section) {
45
+ var widths = (0, _utils.mapChildren)(section, function (column) {
46
+ return column.attrs.width;
47
+ }).join(',');
48
+ switch (widths) {
49
+ case '100':
50
+ return 'single';
51
+ case '33.33,33.33,33.33':
52
+ return 'three_equal';
53
+ case '25,50,25':
54
+ return 'three_with_sidebars';
55
+ case '50,50':
56
+ return 'two_equal';
57
+ case '33.33,66.66':
58
+ return 'two_left_sidebar';
59
+ case '66.66,33.33':
60
+ return 'two_right_sidebar';
61
+ }
62
+ return;
63
+ };
64
+ var getSelectedLayout = exports.getSelectedLayout = function getSelectedLayout(maybeLayoutSection, current) {
65
+ if (maybeLayoutSection && getPresetLayout(maybeLayoutSection)) {
66
+ return getPresetLayout(maybeLayoutSection) || current;
67
+ }
68
+ return current;
69
+ };
70
+ var createDefaultLayoutSection = exports.createDefaultLayoutSection = function createDefaultLayoutSection(state) {
71
+ var _state$schema$nodes = state.schema.nodes,
72
+ layoutSection = _state$schema$nodes.layoutSection,
73
+ layoutColumn = _state$schema$nodes.layoutColumn;
74
+
75
+ // create a 50-50 layout by default
76
+ var columns = _model.Fragment.fromArray([layoutColumn.createAndFill({
77
+ width: 50
78
+ }), layoutColumn.createAndFill({
79
+ width: 50
80
+ })]);
81
+ return layoutSection.createAndFill(undefined, columns);
82
+ };
83
+ var insertLayoutColumns = exports.insertLayoutColumns = function insertLayoutColumns(state, dispatch) {
84
+ if (dispatch) {
85
+ dispatch((0, _utils2.safeInsert)(createDefaultLayoutSection(state))(state.tr));
86
+ }
87
+ return true;
88
+ };
89
+ var insertLayoutColumnsWithAnalytics = exports.insertLayoutColumnsWithAnalytics = function insertLayoutColumnsWithAnalytics(editorAnalyticsAPI) {
90
+ return function (inputMethod) {
91
+ return (0, _editorAnalytics.withAnalytics)(editorAnalyticsAPI, {
92
+ action: _analytics.ACTION.INSERTED,
93
+ actionSubject: _analytics.ACTION_SUBJECT.DOCUMENT,
94
+ actionSubjectId: _analytics.ACTION_SUBJECT_ID.LAYOUT,
95
+ attributes: {
96
+ inputMethod: inputMethod
97
+ },
98
+ eventType: _analytics.EVENT_TYPE.TRACK
99
+ })(insertLayoutColumns);
100
+ };
101
+ };
102
+
103
+ /**
104
+ * Add a column to the right of existing layout
105
+ */
106
+ function addColumn(schema, pos) {
107
+ return function (tr) {
108
+ tr.replaceWith(tr.mapping.map(pos), tr.mapping.map(pos), schema.nodes.layoutColumn.createAndFill());
109
+ };
110
+ }
111
+ function removeLastColumnInLayout(column, columnPos, insideRightEdgePos) {
112
+ return function (tr) {
113
+ if ((0, _utils.isEmptyDocument)(column)) {
114
+ tr.replaceRange(tr.mapping.map(columnPos - 1), tr.mapping.map(insideRightEdgePos), _model.Slice.empty);
115
+ } else {
116
+ tr.replaceRange(tr.mapping.map(columnPos - 1), tr.mapping.map(columnPos + 1), _model.Slice.empty);
117
+ }
118
+ };
119
+ }
120
+ var fromTwoColsToThree = addColumn;
121
+ var fromOneColToTwo = addColumn;
122
+ var fromTwoColsToOne = removeLastColumnInLayout;
123
+ var fromThreeColsToTwo = removeLastColumnInLayout;
124
+ var fromOneColToThree = function fromOneColToThree(schema, pos) {
125
+ return function (tr) {
126
+ addColumn(schema, pos)(tr);
127
+ addColumn(schema, pos)(tr);
128
+ };
129
+ };
130
+ var fromThreeColstoOne = function fromThreeColstoOne(node, tr, insideRightEdgePos) {
131
+ var thirdColumn = node.content.child(2);
132
+ fromThreeColsToTwo(thirdColumn, insideRightEdgePos - thirdColumn.nodeSize, insideRightEdgePos)(tr);
133
+ var secondColumn = node.content.child(1);
134
+ fromTwoColsToOne(secondColumn, insideRightEdgePos - thirdColumn.nodeSize - secondColumn.nodeSize, insideRightEdgePos)(tr);
135
+ };
136
+
137
+ /**
138
+ * Handles switching from 2 -> 3 cols, or 3 -> 2 cols
139
+ * Switching from 2 -> 3 just adds a new one at the end
140
+ * Switching from 3 -> 2 moves all the content of the third col inside the second before
141
+ * removing it
142
+ */
143
+ function forceColumnStructure(state, node, pos, presetLayout) {
144
+ var tr = state.tr;
145
+ var insideRightEdgeOfLayoutSection = pos + node.nodeSize - 1;
146
+ var numCols = node.childCount;
147
+
148
+ // 3 columns -> 2 columns
149
+ if (TWO_COL_LAYOUTS.indexOf(presetLayout) >= 0 && numCols === 3) {
150
+ var thirdColumn = node.content.child(2);
151
+ var columnPos = insideRightEdgeOfLayoutSection - thirdColumn.nodeSize;
152
+ fromThreeColsToTwo(thirdColumn, columnPos, insideRightEdgeOfLayoutSection)(tr);
153
+
154
+ // 2 columns -> 3 columns
155
+ } else if (THREE_COL_LAYOUTS.indexOf(presetLayout) >= 0 && numCols === 2) {
156
+ fromTwoColsToThree(state.schema, insideRightEdgeOfLayoutSection)(tr);
157
+
158
+ // 2 columns -> 1 column
159
+ } else if (ONE_COL_LAYOUTS.indexOf(presetLayout) >= 0 && numCols === 2) {
160
+ var secondColumn = node.content.child(1);
161
+ var _columnPos = insideRightEdgeOfLayoutSection - secondColumn.nodeSize;
162
+ fromTwoColsToOne(secondColumn, _columnPos, insideRightEdgeOfLayoutSection)(tr);
163
+
164
+ // 3 columns -> 1 column
165
+ } else if (ONE_COL_LAYOUTS.indexOf(presetLayout) >= 0 && numCols === 3) {
166
+ fromThreeColstoOne(node, tr, insideRightEdgeOfLayoutSection);
167
+
168
+ // 1 column -> 2 columns
169
+ } else if (TWO_COL_LAYOUTS.indexOf(presetLayout) >= 0 && numCols === 1) {
170
+ fromOneColToTwo(state.schema, insideRightEdgeOfLayoutSection)(tr);
171
+ // 1 column -> 3 columns
172
+ } else if (THREE_COL_LAYOUTS.indexOf(presetLayout) >= 0 && numCols === 1) {
173
+ fromOneColToThree(state.schema, insideRightEdgeOfLayoutSection)(tr);
174
+ }
175
+ return tr;
176
+ }
177
+ function columnWidth(node, schema, widths) {
178
+ var layoutColumn = schema.nodes.layoutColumn;
179
+ var truncatedWidths = widths.map(function (w) {
180
+ return Number(w.toFixed(2));
181
+ });
182
+ return (0, _utils.flatmap)(node.content, function (column, idx) {
183
+ if (column.type === layoutColumn) {
184
+ return layoutColumn.create(_objectSpread(_objectSpread({}, column.attrs), {}, {
185
+ width: truncatedWidths[idx]
186
+ }), column.content, column.marks);
187
+ } else {
188
+ return column;
189
+ }
190
+ });
191
+ }
192
+ function forceColumnWidths(state, tr, pos, presetLayout) {
193
+ var node = tr.doc.nodeAt(pos);
194
+ if (!node) {
195
+ return tr;
196
+ }
197
+ return tr.replaceWith(pos + 1, pos + node.nodeSize - 1, columnWidth(node, state.schema, getWidthsForPreset(presetLayout)));
198
+ }
199
+ function forceSectionToPresetLayout(state, node, pos, presetLayout) {
200
+ var tr = forceColumnStructure(state, node, pos, presetLayout);
201
+
202
+ // save the selection here, since forcing column widths causes a change over the
203
+ // entire layoutSection, which remaps selection to the end. not remapping here
204
+ // is safe because the structure is no longer changing.
205
+ var selection = tr.selection;
206
+ tr = forceColumnWidths(state, tr, pos, presetLayout);
207
+ var selectionPos$ = tr.doc.resolve(selection.$from.pos);
208
+ return tr.setSelection(state.selection instanceof _state.NodeSelection ? new _state.NodeSelection(selectionPos$) : new _state.TextSelection(selectionPos$));
209
+ }
210
+ var setPresetLayout = exports.setPresetLayout = function setPresetLayout(editorAnalyticsAPI) {
211
+ return function (layout) {
212
+ return function (state, dispatch) {
213
+ var _ref = _pluginKey.pluginKey.getState(state),
214
+ pos = _ref.pos,
215
+ selectedLayout = _ref.selectedLayout;
216
+ if (selectedLayout === layout || pos === null) {
217
+ return false;
218
+ }
219
+ var node = state.doc.nodeAt(pos);
220
+ if (!node) {
221
+ return false;
222
+ }
223
+ var tr = forceSectionToPresetLayout(state, node, pos, layout);
224
+ if (tr) {
225
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
226
+ action: _analytics.ACTION.CHANGED_LAYOUT,
227
+ actionSubject: _analytics.ACTION_SUBJECT.LAYOUT,
228
+ attributes: {
229
+ previousLayout: formatLayoutName(selectedLayout),
230
+ newLayout: formatLayoutName(layout)
231
+ },
232
+ eventType: _analytics.EVENT_TYPE.TRACK
233
+ })(tr);
234
+ tr.setMeta('scrollIntoView', false);
235
+ if (dispatch) {
236
+ dispatch(tr);
237
+ }
238
+ return true;
239
+ }
240
+ return false;
241
+ };
242
+ };
243
+ };
244
+ function layoutNeedChanges(node) {
245
+ return !getPresetLayout(node);
246
+ }
247
+ function getLayoutChange(node, pos, schema) {
248
+ if (node.type === schema.nodes.layoutSection) {
249
+ if (!layoutNeedChanges(node)) {
250
+ return;
251
+ }
252
+ var presetLayout = node.childCount === 2 ? 'two_equal' : node.childCount === 3 ? 'three_equal' : 'single';
253
+ var fixedColumns = columnWidth(node, schema, getWidthsForPreset(presetLayout));
254
+ return {
255
+ from: pos + 1,
256
+ to: pos + node.nodeSize - 1,
257
+ slice: new _model.Slice(fixedColumns, 0, 0)
258
+ };
259
+ }
260
+ }
261
+ var fixColumnSizes = exports.fixColumnSizes = function fixColumnSizes(changedTr, state) {
262
+ var layoutSection = state.schema.nodes.layoutSection;
263
+ var change;
264
+ var range = (0, _utils.getStepRange)(changedTr);
265
+ if (!range) {
266
+ return undefined;
267
+ }
268
+ changedTr.doc.nodesBetween(range.from, range.to, function (node, pos) {
269
+ if (node.type !== layoutSection) {
270
+ return true; // Check all internal nodes expect for layout section
271
+ }
272
+ // Node is a section
273
+ if (layoutNeedChanges(node)) {
274
+ change = getLayoutChange(node, pos, state.schema);
275
+ }
276
+ return false; // We dont go deep, We dont accept nested layouts
277
+ });
278
+
279
+ // Hack to prevent: https://product-fabric.atlassian.net/browse/ED-7523
280
+ // By default prosemirror try to recreate the node with the default attributes
281
+ // The default attribute is invalid adf though. when this happen the node after
282
+ // current position is a layout section
283
+ var $pos = changedTr.doc.resolve(range.to);
284
+ if ($pos.depth > 0) {
285
+ // 'range.to' position could resolve to doc, in this ResolvedPos.after will throws
286
+ var pos = $pos.after();
287
+ var node = changedTr.doc.nodeAt(pos);
288
+ if (node && node.type === layoutSection && layoutNeedChanges(node)) {
289
+ change = getLayoutChange(node, pos, state.schema);
290
+ }
291
+ }
292
+ return change;
293
+ };
294
+ var fixColumnStructure = exports.fixColumnStructure = function fixColumnStructure(state) {
295
+ var _ref2 = _pluginKey.pluginKey.getState(state),
296
+ pos = _ref2.pos,
297
+ selectedLayout = _ref2.selectedLayout;
298
+ if (pos !== null && selectedLayout) {
299
+ var node = state.doc.nodeAt(pos);
300
+ if (node && node.childCount !== getWidthsForPreset(selectedLayout).length) {
301
+ return forceSectionToPresetLayout(state, node, pos, selectedLayout);
302
+ }
303
+ }
304
+ return;
305
+ };
306
+ var deleteActiveLayoutNode = exports.deleteActiveLayoutNode = function deleteActiveLayoutNode(editorAnalyticsAPI) {
307
+ return function (state, dispatch) {
308
+ var _ref3 = _pluginKey.pluginKey.getState(state),
309
+ pos = _ref3.pos,
310
+ selectedLayout = _ref3.selectedLayout;
311
+ if (pos !== null) {
312
+ var node = state.doc.nodeAt(pos);
313
+ if (dispatch) {
314
+ var tr = state.tr.delete(pos, pos + node.nodeSize);
315
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
316
+ action: _analytics.ACTION.DELETED,
317
+ actionSubject: _analytics.ACTION_SUBJECT.LAYOUT,
318
+ attributes: {
319
+ layout: formatLayoutName(selectedLayout)
320
+ },
321
+ eventType: _analytics.EVENT_TYPE.TRACK
322
+ })(tr);
323
+ dispatch(tr);
324
+ }
325
+ return true;
326
+ }
327
+ return false;
328
+ };
329
+ };
330
+ var formatLayoutName = function formatLayoutName(layout) {
331
+ switch (layout) {
332
+ case 'single':
333
+ return _analytics.LAYOUT_TYPE.SINGLE_COL;
334
+ case 'two_equal':
335
+ return _analytics.LAYOUT_TYPE.TWO_COLS_EQUAL;
336
+ case 'three_equal':
337
+ return _analytics.LAYOUT_TYPE.THREE_COLS_EQUAL;
338
+ case 'two_left_sidebar':
339
+ return _analytics.LAYOUT_TYPE.LEFT_SIDEBAR;
340
+ case 'two_right_sidebar':
341
+ return _analytics.LAYOUT_TYPE.RIGHT_SIDEBAR;
342
+ case 'three_with_sidebars':
343
+ return _analytics.LAYOUT_TYPE.THREE_WITH_SIDEBARS;
344
+ }
345
+ };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "layoutPlugin", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _plugin.layoutPlugin;
10
+ }
11
+ });
12
+ var _plugin = require("./plugin");
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.layoutPlugin = void 0;
8
+ Object.defineProperty(exports, "pluginKey", {
9
+ enumerable: true,
10
+ get: function get() {
11
+ return _pluginKey.pluginKey;
12
+ }
13
+ });
14
+ var _react = _interopRequireDefault(require("react"));
15
+ var _adfSchema = require("@atlaskit/adf-schema");
16
+ var _analytics = require("@atlaskit/editor-common/analytics");
17
+ var _messages = require("@atlaskit/editor-common/messages");
18
+ var _quickInsert = require("@atlaskit/editor-common/quick-insert");
19
+ var _actions = require("./actions");
20
+ var _main = _interopRequireDefault(require("./pm-plugins/main"));
21
+ var _pluginKey = require("./pm-plugins/plugin-key");
22
+ var _toolbar = require("./toolbar");
23
+ var layoutPlugin = exports.layoutPlugin = function layoutPlugin(_ref) {
24
+ var _api$analytics;
25
+ var _ref$config = _ref.config,
26
+ options = _ref$config === void 0 ? {} : _ref$config,
27
+ api = _ref.api;
28
+ return {
29
+ name: 'layout',
30
+ nodes: function nodes() {
31
+ return [{
32
+ name: 'layoutSection',
33
+ node: _adfSchema.layoutSection
34
+ }, {
35
+ name: 'layoutColumn',
36
+ node: _adfSchema.layoutColumn
37
+ }];
38
+ },
39
+ pmPlugins: function pmPlugins() {
40
+ return [{
41
+ name: 'layout',
42
+ plugin: function plugin() {
43
+ return (0, _main.default)(options);
44
+ }
45
+ }];
46
+ },
47
+ actions: {
48
+ insertLayoutColumns: (0, _actions.insertLayoutColumnsWithAnalytics)(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)
49
+ },
50
+ pluginsOptions: {
51
+ floatingToolbar: function floatingToolbar(state, intl) {
52
+ var _ref2 = _pluginKey.pluginKey.getState(state),
53
+ pos = _ref2.pos,
54
+ allowBreakout = _ref2.allowBreakout,
55
+ addSidebarLayouts = _ref2.addSidebarLayouts,
56
+ allowSingleColumnLayout = _ref2.allowSingleColumnLayout;
57
+ if (pos !== null) {
58
+ return (0, _toolbar.buildToolbar)(state, intl, pos, allowBreakout, addSidebarLayouts, allowSingleColumnLayout, api);
59
+ }
60
+ return undefined;
61
+ },
62
+ quickInsert: function quickInsert(_ref3) {
63
+ var formatMessage = _ref3.formatMessage;
64
+ return [{
65
+ id: 'layout',
66
+ title: formatMessage(_messages.toolbarInsertBlockMessages.columns),
67
+ description: formatMessage(_messages.toolbarInsertBlockMessages.columnsDescription),
68
+ keywords: ['column', 'section'],
69
+ priority: 1100,
70
+ icon: function icon() {
71
+ return /*#__PURE__*/_react.default.createElement(_quickInsert.IconLayout, null);
72
+ },
73
+ action: function action(insert, state) {
74
+ var _api$analytics2;
75
+ var tr = insert((0, _actions.createDefaultLayoutSection)(state));
76
+ api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 || (_api$analytics2 = _api$analytics2.actions) === null || _api$analytics2 === void 0 || _api$analytics2.attachAnalyticsEvent({
77
+ action: _analytics.ACTION.INSERTED,
78
+ actionSubject: _analytics.ACTION_SUBJECT.DOCUMENT,
79
+ actionSubjectId: _analytics.ACTION_SUBJECT_ID.LAYOUT,
80
+ attributes: {
81
+ inputMethod: _analytics.INPUT_METHOD.QUICK_INSERT
82
+ },
83
+ eventType: _analytics.EVENT_TYPE.TRACK
84
+ })(tr);
85
+ return tr;
86
+ }
87
+ }];
88
+ }
89
+ }
90
+ };
91
+ };
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = exports.DEFAULT_LAYOUT = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
10
+ var _selection = require("@atlaskit/editor-common/selection");
11
+ var _utils = require("@atlaskit/editor-common/utils");
12
+ var _keymap = require("@atlaskit/editor-prosemirror/keymap");
13
+ var _state = require("@atlaskit/editor-prosemirror/state");
14
+ var _utils2 = require("@atlaskit/editor-prosemirror/utils");
15
+ var _view = require("@atlaskit/editor-prosemirror/view");
16
+ var _actions = require("../actions");
17
+ var _pluginKey = require("./plugin-key");
18
+ 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; }
19
+ 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) { (0, _defineProperty2.default)(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; }
20
+ var DEFAULT_LAYOUT = exports.DEFAULT_LAYOUT = 'two_equal';
21
+ var isWholeSelectionInsideLayoutColumn = function isWholeSelectionInsideLayoutColumn(state) {
22
+ // Since findParentNodeOfType doesn't check if selection.to shares the parent, we do this check ourselves
23
+ var fromParent = (0, _utils2.findParentNodeOfType)(state.schema.nodes.layoutColumn)(state.selection);
24
+ if (fromParent) {
25
+ var isToPosInsideSameLayoutColumn = state.selection.from < fromParent.pos + fromParent.node.nodeSize;
26
+ return isToPosInsideSameLayoutColumn;
27
+ }
28
+ return false;
29
+ };
30
+ var moveCursorToNextColumn = function moveCursorToNextColumn(state, dispatch) {
31
+ var selection = state.selection;
32
+ var _state$schema$nodes = state.schema.nodes,
33
+ layoutColumn = _state$schema$nodes.layoutColumn,
34
+ layoutSection = _state$schema$nodes.layoutSection;
35
+ var section = (0, _utils2.findParentNodeOfType)(layoutSection)(selection);
36
+ var column = (0, _utils2.findParentNodeOfType)(layoutColumn)(selection);
37
+ if (column.node !== section.node.lastChild) {
38
+ var $nextColumn = state.doc.resolve(column.pos + column.node.nodeSize);
39
+ var shiftedSelection = _state.TextSelection.findFrom($nextColumn, 1);
40
+ if (dispatch) {
41
+ dispatch(state.tr.setSelection(shiftedSelection));
42
+ }
43
+ }
44
+ return true;
45
+ };
46
+
47
+ // TODO: Look at memoize-one-ing this fn
48
+ var getNodeDecoration = function getNodeDecoration(pos, node) {
49
+ return [_view.Decoration.node(pos, pos + node.nodeSize, {
50
+ class: 'selected'
51
+ })];
52
+ };
53
+ var getInitialPluginState = function getInitialPluginState(options, state) {
54
+ var maybeLayoutSection = (0, _utils2.findParentNodeOfType)(state.schema.nodes.layoutSection)(state.selection);
55
+ var allowBreakout = options.allowBreakout || false;
56
+ var addSidebarLayouts = options.UNSAFE_addSidebarLayouts || false;
57
+ var allowSingleColumnLayout = options.UNSAFE_allowSingleColumnLayout || false;
58
+ var pos = maybeLayoutSection ? maybeLayoutSection.pos : null;
59
+ var selectedLayout = (0, _actions.getSelectedLayout)(maybeLayoutSection && maybeLayoutSection.node, DEFAULT_LAYOUT);
60
+ return {
61
+ pos: pos,
62
+ allowBreakout: allowBreakout,
63
+ addSidebarLayouts: addSidebarLayouts,
64
+ selectedLayout: selectedLayout,
65
+ allowSingleColumnLayout: allowSingleColumnLayout
66
+ };
67
+ };
68
+ var _default = exports.default = function _default(options) {
69
+ return new _safePlugin.SafePlugin({
70
+ key: _pluginKey.pluginKey,
71
+ state: {
72
+ init: function init(_, state) {
73
+ return getInitialPluginState(options, state);
74
+ },
75
+ apply: function apply(tr, pluginState, _oldState, newState) {
76
+ if (tr.docChanged || tr.selectionSet) {
77
+ var layoutSection = newState.schema.nodes.layoutSection,
78
+ selection = newState.selection;
79
+ var maybeLayoutSection = (0, _utils2.findParentNodeOfType)(layoutSection)(selection) || (0, _utils2.findSelectedNodeOfType)([layoutSection])(selection);
80
+ var newPluginState = _objectSpread(_objectSpread({}, pluginState), {}, {
81
+ pos: maybeLayoutSection ? maybeLayoutSection.pos : null,
82
+ selectedLayout: (0, _actions.getSelectedLayout)(maybeLayoutSection && maybeLayoutSection.node, pluginState.selectedLayout)
83
+ });
84
+ return newPluginState;
85
+ }
86
+ return pluginState;
87
+ }
88
+ },
89
+ props: {
90
+ decorations: function decorations(state) {
91
+ var layoutState = _pluginKey.pluginKey.getState(state);
92
+ if (layoutState.pos !== null) {
93
+ return _view.DecorationSet.create(state.doc, getNodeDecoration(layoutState.pos, state.doc.nodeAt(layoutState.pos)));
94
+ }
95
+ return undefined;
96
+ },
97
+ handleKeyDown: (0, _keymap.keydownHandler)({
98
+ Tab: (0, _utils.filterCommand)(isWholeSelectionInsideLayoutColumn, moveCursorToNextColumn)
99
+ }),
100
+ handleClickOn: (0, _selection.createSelectionClickHandler)(['layoutColumn'], function (target) {
101
+ return target.hasAttribute('data-layout-section') || target.hasAttribute('data-layout-column');
102
+ }, {
103
+ useLongPressSelection: options.useLongPressSelection || false,
104
+ getNodeSelectionPos: function getNodeSelectionPos(state, nodePos) {
105
+ return state.doc.resolve(nodePos).before();
106
+ }
107
+ })
108
+ },
109
+ appendTransaction: function appendTransaction(transactions, _oldState, newState) {
110
+ var changes = [];
111
+ transactions.forEach(function (prevTr) {
112
+ // remap change segments across the transaction set
113
+ changes.forEach(function (change) {
114
+ return {
115
+ from: prevTr.mapping.map(change.from),
116
+ to: prevTr.mapping.map(change.to),
117
+ slice: change.slice
118
+ };
119
+ });
120
+
121
+ // don't consider transactions that don't mutate
122
+ if (!prevTr.docChanged) {
123
+ return;
124
+ }
125
+ var change = (0, _actions.fixColumnSizes)(prevTr, newState);
126
+ if (change) {
127
+ changes.push(change);
128
+ }
129
+ });
130
+ if (changes.length) {
131
+ var tr = newState.tr;
132
+ var selection = newState.selection.toJSON();
133
+ changes.forEach(function (change) {
134
+ tr.replaceRange(change.from, change.to, change.slice);
135
+ });
136
+
137
+ // selecting and deleting across columns in 3 col layouts can remove
138
+ // a layoutColumn so we fix the structure here
139
+ tr = (0, _actions.fixColumnStructure)(newState) || tr;
140
+ if (tr.docChanged) {
141
+ tr.setSelection(_state.Selection.fromJSON(tr.doc, selection));
142
+ return tr;
143
+ }
144
+ }
145
+ return;
146
+ }
147
+ });
148
+ };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.pluginKey = void 0;
7
+ var _state = require("@atlaskit/editor-prosemirror/state");
8
+ var pluginKey = exports.pluginKey = new _state.PluginKey('layout');
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });