@atlaskit/editor-plugin-layout 1.12.5 → 1.12.6

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @atlaskit/editor-plugin-layout
2
2
 
3
+ ## 1.12.6
4
+
5
+ ### Patch Changes
6
+
7
+ - [#169290](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/169290)
8
+ [`128ec4e2667bc`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/128ec4e2667bc) -
9
+ ED-25824 fix invalid layout after undo or other operations.
10
+
3
11
  ## 1.12.5
4
12
 
5
13
  ### Patch Changes
@@ -15,6 +15,7 @@ var _utils = require("@atlaskit/editor-common/utils");
15
15
  var _model = require("@atlaskit/editor-prosemirror/model");
16
16
  var _state = require("@atlaskit/editor-prosemirror/state");
17
17
  var _utils2 = require("@atlaskit/editor-prosemirror/utils");
18
+ var _experiments = require("@atlaskit/tmp-editor-statsig/experiments");
18
19
  var _consts = require("./consts");
19
20
  var _pluginKey = require("./pm-plugins/plugin-key");
20
21
  var _preRelease = require("./utils/preRelease");
@@ -65,11 +66,17 @@ var getWidthsForPreset = function getWidthsForPreset(presetLayout) {
65
66
  return [];
66
67
  };
67
68
  var isValidLayoutWidthDistributions = function isValidLayoutWidthDistributions(layoutSection) {
68
- var totalWidth = (0, _utils.mapChildren)(layoutSection, function (column) {
69
+ var totalWidth = 0;
70
+ (0, _utils.mapChildren)(layoutSection, function (column) {
69
71
  return column.attrs.width;
70
- }).reduce(function (total, width) {
71
- return total + width;
72
- }, 0);
72
+ }).forEach(function (width) {
73
+ if (typeof width === 'number' && isFinite(width) && width > 0 && width <= 100) {
74
+ totalWidth += width;
75
+ }
76
+
77
+ // not a valid width, e.g. 0, 100, undefined
78
+ return false;
79
+ });
73
80
  return Math.round(totalWidth) === 100;
74
81
  };
75
82
 
@@ -389,12 +396,30 @@ function layoutNeedChanges(node) {
389
396
  }
390
397
  return !getPresetLayout(node);
391
398
  }
399
+ var getDefaultPresetLayout = function getDefaultPresetLayout(layoutNode) {
400
+ var layoutColumnCount = layoutNode.childCount;
401
+ if (layoutColumnCount <= 1) {
402
+ return 'single';
403
+ }
404
+ switch (layoutColumnCount) {
405
+ case 2:
406
+ return 'two_equal';
407
+ case 3:
408
+ return 'three_equal';
409
+ case 4:
410
+ return 'four_equal';
411
+ case 5:
412
+ return 'five_equal';
413
+ default:
414
+ return 'five_equal';
415
+ }
416
+ };
392
417
  function getLayoutChange(node, pos, schema) {
393
418
  if (node.type === schema.nodes.layoutSection) {
394
419
  if (!layoutNeedChanges(node)) {
395
420
  return;
396
421
  }
397
- var presetLayout = node.childCount === 2 ? 'two_equal' : node.childCount === 3 ? 'three_equal' : 'single';
422
+ var presetLayout = (0, _experiments.editorExperiment)('advanced_layouts', true) ? getDefaultPresetLayout(node) : node.childCount === 2 ? 'two_equal' : node.childCount === 3 ? 'three_equal' : 'single';
398
423
  var fixedColumns = columnWidth(node, schema, getWidthsForPreset(presetLayout));
399
424
  return {
400
425
  from: pos + 1,
@@ -443,8 +468,13 @@ var fixColumnStructure = exports.fixColumnStructure = function fixColumnStructur
443
468
  selectedLayout = _ref2.selectedLayout;
444
469
  if (pos !== null && selectedLayout) {
445
470
  var node = state.doc.nodeAt(pos);
446
- if (node && node.childCount !== getWidthsForPreset(selectedLayout).length) {
447
- return forceSectionToPresetLayout(state, node, pos, selectedLayout);
471
+ if (node) {
472
+ if (node.childCount !== getWidthsForPreset(selectedLayout).length) {
473
+ return forceSectionToPresetLayout(state, node, pos, selectedLayout);
474
+ }
475
+ if (!isValidLayoutWidthDistributions(node) && (0, _experiments.editorExperiment)('advanced_layouts', true)) {
476
+ return forceSectionToPresetLayout(state, node, pos, selectedLayout);
477
+ }
448
478
  }
449
479
  }
450
480
  return;
@@ -5,6 +5,7 @@ import { flatmap, getStepRange, isEmptyDocument, mapChildren } from '@atlaskit/e
5
5
  import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
6
6
  import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
7
7
  import { safeInsert } from '@atlaskit/editor-prosemirror/utils';
8
+ import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
8
9
  import { EVEN_DISTRIBUTED_COL_WIDTHS } from './consts';
9
10
  import { pluginKey } from './pm-plugins/plugin-key';
10
11
  import { isPreRelease2 } from './utils/preRelease';
@@ -53,9 +54,15 @@ const getWidthsForPreset = presetLayout => {
53
54
  return [];
54
55
  };
55
56
  const isValidLayoutWidthDistributions = layoutSection => {
56
- const totalWidth = mapChildren(layoutSection, column => column.attrs.width).reduce((total, width) => {
57
- return total + width;
58
- }, 0);
57
+ let totalWidth = 0;
58
+ mapChildren(layoutSection, column => column.attrs.width).forEach(width => {
59
+ if (typeof width === 'number' && isFinite(width) && width > 0 && width <= 100) {
60
+ totalWidth += width;
61
+ }
62
+
63
+ // not a valid width, e.g. 0, 100, undefined
64
+ return false;
65
+ });
59
66
  return Math.round(totalWidth) === 100;
60
67
  };
61
68
 
@@ -365,12 +372,30 @@ function layoutNeedChanges(node) {
365
372
  }
366
373
  return !getPresetLayout(node);
367
374
  }
375
+ const getDefaultPresetLayout = layoutNode => {
376
+ const layoutColumnCount = layoutNode.childCount;
377
+ if (layoutColumnCount <= 1) {
378
+ return 'single';
379
+ }
380
+ switch (layoutColumnCount) {
381
+ case 2:
382
+ return 'two_equal';
383
+ case 3:
384
+ return 'three_equal';
385
+ case 4:
386
+ return 'four_equal';
387
+ case 5:
388
+ return 'five_equal';
389
+ default:
390
+ return 'five_equal';
391
+ }
392
+ };
368
393
  function getLayoutChange(node, pos, schema) {
369
394
  if (node.type === schema.nodes.layoutSection) {
370
395
  if (!layoutNeedChanges(node)) {
371
396
  return;
372
397
  }
373
- const presetLayout = node.childCount === 2 ? 'two_equal' : node.childCount === 3 ? 'three_equal' : 'single';
398
+ const presetLayout = editorExperiment('advanced_layouts', true) ? getDefaultPresetLayout(node) : node.childCount === 2 ? 'two_equal' : node.childCount === 3 ? 'three_equal' : 'single';
374
399
  const fixedColumns = columnWidth(node, schema, getWidthsForPreset(presetLayout));
375
400
  return {
376
401
  from: pos + 1,
@@ -422,8 +447,13 @@ export const fixColumnStructure = state => {
422
447
  } = pluginKey.getState(state);
423
448
  if (pos !== null && selectedLayout) {
424
449
  const node = state.doc.nodeAt(pos);
425
- if (node && node.childCount !== getWidthsForPreset(selectedLayout).length) {
426
- return forceSectionToPresetLayout(state, node, pos, selectedLayout);
450
+ if (node) {
451
+ if (node.childCount !== getWidthsForPreset(selectedLayout).length) {
452
+ return forceSectionToPresetLayout(state, node, pos, selectedLayout);
453
+ }
454
+ if (!isValidLayoutWidthDistributions(node) && editorExperiment('advanced_layouts', true)) {
455
+ return forceSectionToPresetLayout(state, node, pos, selectedLayout);
456
+ }
427
457
  }
428
458
  }
429
459
  return;
@@ -8,6 +8,7 @@ import { flatmap, getStepRange, isEmptyDocument, mapChildren } from '@atlaskit/e
8
8
  import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model';
9
9
  import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
10
10
  import { safeInsert } from '@atlaskit/editor-prosemirror/utils';
11
+ import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
11
12
  import { EVEN_DISTRIBUTED_COL_WIDTHS } from './consts';
12
13
  import { pluginKey } from './pm-plugins/plugin-key';
13
14
  import { isPreRelease2 } from './utils/preRelease';
@@ -56,11 +57,17 @@ var getWidthsForPreset = function getWidthsForPreset(presetLayout) {
56
57
  return [];
57
58
  };
58
59
  var isValidLayoutWidthDistributions = function isValidLayoutWidthDistributions(layoutSection) {
59
- var totalWidth = mapChildren(layoutSection, function (column) {
60
+ var totalWidth = 0;
61
+ mapChildren(layoutSection, function (column) {
60
62
  return column.attrs.width;
61
- }).reduce(function (total, width) {
62
- return total + width;
63
- }, 0);
63
+ }).forEach(function (width) {
64
+ if (typeof width === 'number' && isFinite(width) && width > 0 && width <= 100) {
65
+ totalWidth += width;
66
+ }
67
+
68
+ // not a valid width, e.g. 0, 100, undefined
69
+ return false;
70
+ });
64
71
  return Math.round(totalWidth) === 100;
65
72
  };
66
73
 
@@ -380,12 +387,30 @@ function layoutNeedChanges(node) {
380
387
  }
381
388
  return !getPresetLayout(node);
382
389
  }
390
+ var getDefaultPresetLayout = function getDefaultPresetLayout(layoutNode) {
391
+ var layoutColumnCount = layoutNode.childCount;
392
+ if (layoutColumnCount <= 1) {
393
+ return 'single';
394
+ }
395
+ switch (layoutColumnCount) {
396
+ case 2:
397
+ return 'two_equal';
398
+ case 3:
399
+ return 'three_equal';
400
+ case 4:
401
+ return 'four_equal';
402
+ case 5:
403
+ return 'five_equal';
404
+ default:
405
+ return 'five_equal';
406
+ }
407
+ };
383
408
  function getLayoutChange(node, pos, schema) {
384
409
  if (node.type === schema.nodes.layoutSection) {
385
410
  if (!layoutNeedChanges(node)) {
386
411
  return;
387
412
  }
388
- var presetLayout = node.childCount === 2 ? 'two_equal' : node.childCount === 3 ? 'three_equal' : 'single';
413
+ var presetLayout = editorExperiment('advanced_layouts', true) ? getDefaultPresetLayout(node) : node.childCount === 2 ? 'two_equal' : node.childCount === 3 ? 'three_equal' : 'single';
389
414
  var fixedColumns = columnWidth(node, schema, getWidthsForPreset(presetLayout));
390
415
  return {
391
416
  from: pos + 1,
@@ -434,8 +459,13 @@ export var fixColumnStructure = function fixColumnStructure(state) {
434
459
  selectedLayout = _ref2.selectedLayout;
435
460
  if (pos !== null && selectedLayout) {
436
461
  var node = state.doc.nodeAt(pos);
437
- if (node && node.childCount !== getWidthsForPreset(selectedLayout).length) {
438
- return forceSectionToPresetLayout(state, node, pos, selectedLayout);
462
+ if (node) {
463
+ if (node.childCount !== getWidthsForPreset(selectedLayout).length) {
464
+ return forceSectionToPresetLayout(state, node, pos, selectedLayout);
465
+ }
466
+ if (!isValidLayoutWidthDistributions(node) && editorExperiment('advanced_layouts', true)) {
467
+ return forceSectionToPresetLayout(state, node, pos, selectedLayout);
468
+ }
439
469
  }
440
470
  }
441
471
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-layout",
3
- "version": "1.12.5",
3
+ "version": "1.12.6",
4
4
  "description": "Layout plugin for @atlaskit/editor-core",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",