@atlaskit/editor-plugin-synced-block 4.2.15 → 4.3.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @atlaskit/editor-plugin-synced-block
2
2
 
3
+ ## 4.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`1f699b87203c4`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/1f699b87203c4) -
8
+ EDITOR-2881 improve track sync blocks function
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies
13
+
3
14
  ## 4.2.15
4
15
 
5
16
  ### Patch Changes
@@ -12,56 +12,82 @@ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length)
12
12
  var trackSyncBlocks = exports.trackSyncBlocks = function trackSyncBlocks(predicate, tr, state) {
13
13
  var removed = {};
14
14
  var added = {};
15
+ if (!tr.docChanged) {
16
+ return {
17
+ removed: [],
18
+ added: []
19
+ };
20
+ }
15
21
 
16
22
  // and cast to specific step types
17
23
  var replaceSteps = tr.steps.filter(function (step) {
18
24
  return step instanceof _transform.ReplaceStep || step instanceof _transform.ReplaceAroundStep;
19
25
  });
20
- replaceSteps.forEach(function (step) {
26
+
27
+ // this is a quick check to see if any insertion/deletion of bodiedSyncBlock happened
28
+ var hasBodiedSyncBlockChanges = replaceSteps.some(function (step) {
21
29
  var from = step.from,
22
30
  to = step.to;
23
- // replaced a range, check for deleted syncBlock
24
-
31
+ var hasChange = false;
25
32
  if (from !== to) {
26
33
  step.getMap().forEach(function (oldStart, oldEnd) {
27
- if (oldStart !== oldEnd) {
28
- var deletedSlice = state.doc.slice(oldStart, oldEnd);
29
- deletedSlice.content.nodesBetween(0, deletedSlice.content.size, function (node) {
34
+ if (oldStart !== oldEnd && !hasChange) {
35
+ var deletedSlice = state.doc.slice(Math.max(0, oldStart), Math.min(state.doc.content.size, oldEnd));
36
+ deletedSlice.content.forEach(function (node, _, index) {
37
+ if (hasChange) {
38
+ return;
39
+ }
40
+ // for top level nodes
30
41
  if (predicate(node)) {
31
- if (added[node.attrs.localId]) {
32
- // If a source block added and then removed in the same transaction,
33
- // we treat it as no-op.
34
- delete added[node.attrs.localId];
35
- } else {
36
- removed[node.attrs.localId] = node.attrs;
37
- }
42
+ hasChange = true;
38
43
  }
39
-
40
- // we don't need to go deeper
41
- return false;
42
44
  });
43
45
  }
44
46
  });
45
47
  }
46
48
 
47
- // replaced content, check for inserted syncBlock
48
- // if only one replace step, we have already checked the entire replaced range above
49
- if (step.slice.content.size > 0) {
50
- step.slice.content.nodesBetween(0, step.slice.content.size, function (node) {
51
- if (predicate(node)) {
52
- if (removed[node.attrs.localId]) {
53
- // If a source block is removed and added back in the same transaction,
54
- // we treat it as no-op.
55
- delete removed[node.attrs.localId];
56
- } else {
57
- added[node.attrs.localId] = node.attrs;
58
- }
49
+ // no need to check insertions if we already found deletions
50
+ if (step.slice.content.size > 0 && !hasChange) {
51
+ step.slice.content.forEach(function (node) {
52
+ if (predicate(node) && !hasChange) {
53
+ hasChange = true;
59
54
  }
60
- // we don't need to go deeper
61
- return false;
62
55
  });
63
56
  }
57
+ return hasChange;
64
58
  });
59
+ if (hasBodiedSyncBlockChanges) {
60
+ var oldDoc = state.doc;
61
+ var newDoc = tr.doc;
62
+ var syncBlockMapOld = {};
63
+ var syncBlockMapNew = {};
64
+ oldDoc.content.forEach(function (node) {
65
+ if (predicate(node)) {
66
+ var syncBlockAttr = node.attrs;
67
+ syncBlockMapOld[syncBlockAttr.localId] = syncBlockAttr;
68
+ }
69
+ });
70
+ newDoc.content.forEach(function (node) {
71
+ if (predicate(node)) {
72
+ var syncBlockAttr = node.attrs;
73
+ syncBlockMapNew[syncBlockAttr.localId] = syncBlockAttr;
74
+ }
75
+ });
76
+
77
+ // Find removed sync blocks
78
+ for (var localId in syncBlockMapOld) {
79
+ if (!syncBlockMapNew[localId]) {
80
+ removed[localId] = syncBlockMapOld[localId];
81
+ }
82
+ }
83
+
84
+ // Find added sync blocks
85
+ for (var _localId in syncBlockMapNew) {
86
+ if (!syncBlockMapOld[_localId]) {
87
+ added[_localId] = syncBlockMapNew[_localId];
88
+ }
89
+ }
90
+ }
65
91
  return {
66
92
  removed: Object.values(removed),
67
93
  added: Object.values(added)
@@ -3,56 +3,82 @@ import { findParentNodeOfTypeClosestToPos } from '@atlaskit/editor-prosemirror/u
3
3
  export const trackSyncBlocks = (predicate, tr, state) => {
4
4
  const removed = {};
5
5
  const added = {};
6
+ if (!tr.docChanged) {
7
+ return {
8
+ removed: [],
9
+ added: []
10
+ };
11
+ }
6
12
 
7
13
  // and cast to specific step types
8
14
  const replaceSteps = tr.steps.filter(step => step instanceof ReplaceStep || step instanceof ReplaceAroundStep);
9
- replaceSteps.forEach(step => {
15
+
16
+ // this is a quick check to see if any insertion/deletion of bodiedSyncBlock happened
17
+ const hasBodiedSyncBlockChanges = replaceSteps.some(step => {
10
18
  const {
11
19
  from,
12
20
  to
13
21
  } = step;
14
- // replaced a range, check for deleted syncBlock
15
-
22
+ let hasChange = false;
16
23
  if (from !== to) {
17
24
  step.getMap().forEach((oldStart, oldEnd) => {
18
- if (oldStart !== oldEnd) {
19
- const deletedSlice = state.doc.slice(oldStart, oldEnd);
20
- deletedSlice.content.nodesBetween(0, deletedSlice.content.size, node => {
25
+ if (oldStart !== oldEnd && !hasChange) {
26
+ const deletedSlice = state.doc.slice(Math.max(0, oldStart), Math.min(state.doc.content.size, oldEnd));
27
+ deletedSlice.content.forEach((node, _, index) => {
28
+ if (hasChange) {
29
+ return;
30
+ }
31
+ // for top level nodes
21
32
  if (predicate(node)) {
22
- if (added[node.attrs.localId]) {
23
- // If a source block added and then removed in the same transaction,
24
- // we treat it as no-op.
25
- delete added[node.attrs.localId];
26
- } else {
27
- removed[node.attrs.localId] = node.attrs;
28
- }
33
+ hasChange = true;
29
34
  }
30
-
31
- // we don't need to go deeper
32
- return false;
33
35
  });
34
36
  }
35
37
  });
36
38
  }
37
39
 
38
- // replaced content, check for inserted syncBlock
39
- // if only one replace step, we have already checked the entire replaced range above
40
- if (step.slice.content.size > 0) {
41
- step.slice.content.nodesBetween(0, step.slice.content.size, node => {
42
- if (predicate(node)) {
43
- if (removed[node.attrs.localId]) {
44
- // If a source block is removed and added back in the same transaction,
45
- // we treat it as no-op.
46
- delete removed[node.attrs.localId];
47
- } else {
48
- added[node.attrs.localId] = node.attrs;
49
- }
40
+ // no need to check insertions if we already found deletions
41
+ if (step.slice.content.size > 0 && !hasChange) {
42
+ step.slice.content.forEach(node => {
43
+ if (predicate(node) && !hasChange) {
44
+ hasChange = true;
50
45
  }
51
- // we don't need to go deeper
52
- return false;
53
46
  });
54
47
  }
48
+ return hasChange;
55
49
  });
50
+ if (hasBodiedSyncBlockChanges) {
51
+ const oldDoc = state.doc;
52
+ const newDoc = tr.doc;
53
+ const syncBlockMapOld = {};
54
+ const syncBlockMapNew = {};
55
+ oldDoc.content.forEach(node => {
56
+ if (predicate(node)) {
57
+ const syncBlockAttr = node.attrs;
58
+ syncBlockMapOld[syncBlockAttr.localId] = syncBlockAttr;
59
+ }
60
+ });
61
+ newDoc.content.forEach(node => {
62
+ if (predicate(node)) {
63
+ const syncBlockAttr = node.attrs;
64
+ syncBlockMapNew[syncBlockAttr.localId] = syncBlockAttr;
65
+ }
66
+ });
67
+
68
+ // Find removed sync blocks
69
+ for (const localId in syncBlockMapOld) {
70
+ if (!syncBlockMapNew[localId]) {
71
+ removed[localId] = syncBlockMapOld[localId];
72
+ }
73
+ }
74
+
75
+ // Find added sync blocks
76
+ for (const localId in syncBlockMapNew) {
77
+ if (!syncBlockMapOld[localId]) {
78
+ added[localId] = syncBlockMapNew[localId];
79
+ }
80
+ }
81
+ }
56
82
  return {
57
83
  removed: Object.values(removed),
58
84
  added: Object.values(added)
@@ -6,56 +6,82 @@ import { findParentNodeOfTypeClosestToPos } from '@atlaskit/editor-prosemirror/u
6
6
  export var trackSyncBlocks = function trackSyncBlocks(predicate, tr, state) {
7
7
  var removed = {};
8
8
  var added = {};
9
+ if (!tr.docChanged) {
10
+ return {
11
+ removed: [],
12
+ added: []
13
+ };
14
+ }
9
15
 
10
16
  // and cast to specific step types
11
17
  var replaceSteps = tr.steps.filter(function (step) {
12
18
  return step instanceof ReplaceStep || step instanceof ReplaceAroundStep;
13
19
  });
14
- replaceSteps.forEach(function (step) {
20
+
21
+ // this is a quick check to see if any insertion/deletion of bodiedSyncBlock happened
22
+ var hasBodiedSyncBlockChanges = replaceSteps.some(function (step) {
15
23
  var from = step.from,
16
24
  to = step.to;
17
- // replaced a range, check for deleted syncBlock
18
-
25
+ var hasChange = false;
19
26
  if (from !== to) {
20
27
  step.getMap().forEach(function (oldStart, oldEnd) {
21
- if (oldStart !== oldEnd) {
22
- var deletedSlice = state.doc.slice(oldStart, oldEnd);
23
- deletedSlice.content.nodesBetween(0, deletedSlice.content.size, function (node) {
28
+ if (oldStart !== oldEnd && !hasChange) {
29
+ var deletedSlice = state.doc.slice(Math.max(0, oldStart), Math.min(state.doc.content.size, oldEnd));
30
+ deletedSlice.content.forEach(function (node, _, index) {
31
+ if (hasChange) {
32
+ return;
33
+ }
34
+ // for top level nodes
24
35
  if (predicate(node)) {
25
- if (added[node.attrs.localId]) {
26
- // If a source block added and then removed in the same transaction,
27
- // we treat it as no-op.
28
- delete added[node.attrs.localId];
29
- } else {
30
- removed[node.attrs.localId] = node.attrs;
31
- }
36
+ hasChange = true;
32
37
  }
33
-
34
- // we don't need to go deeper
35
- return false;
36
38
  });
37
39
  }
38
40
  });
39
41
  }
40
42
 
41
- // replaced content, check for inserted syncBlock
42
- // if only one replace step, we have already checked the entire replaced range above
43
- if (step.slice.content.size > 0) {
44
- step.slice.content.nodesBetween(0, step.slice.content.size, function (node) {
45
- if (predicate(node)) {
46
- if (removed[node.attrs.localId]) {
47
- // If a source block is removed and added back in the same transaction,
48
- // we treat it as no-op.
49
- delete removed[node.attrs.localId];
50
- } else {
51
- added[node.attrs.localId] = node.attrs;
52
- }
43
+ // no need to check insertions if we already found deletions
44
+ if (step.slice.content.size > 0 && !hasChange) {
45
+ step.slice.content.forEach(function (node) {
46
+ if (predicate(node) && !hasChange) {
47
+ hasChange = true;
53
48
  }
54
- // we don't need to go deeper
55
- return false;
56
49
  });
57
50
  }
51
+ return hasChange;
58
52
  });
53
+ if (hasBodiedSyncBlockChanges) {
54
+ var oldDoc = state.doc;
55
+ var newDoc = tr.doc;
56
+ var syncBlockMapOld = {};
57
+ var syncBlockMapNew = {};
58
+ oldDoc.content.forEach(function (node) {
59
+ if (predicate(node)) {
60
+ var syncBlockAttr = node.attrs;
61
+ syncBlockMapOld[syncBlockAttr.localId] = syncBlockAttr;
62
+ }
63
+ });
64
+ newDoc.content.forEach(function (node) {
65
+ if (predicate(node)) {
66
+ var syncBlockAttr = node.attrs;
67
+ syncBlockMapNew[syncBlockAttr.localId] = syncBlockAttr;
68
+ }
69
+ });
70
+
71
+ // Find removed sync blocks
72
+ for (var localId in syncBlockMapOld) {
73
+ if (!syncBlockMapNew[localId]) {
74
+ removed[localId] = syncBlockMapOld[localId];
75
+ }
76
+ }
77
+
78
+ // Find added sync blocks
79
+ for (var _localId in syncBlockMapNew) {
80
+ if (!syncBlockMapOld[_localId]) {
81
+ added[_localId] = syncBlockMapNew[_localId];
82
+ }
83
+ }
84
+ }
59
85
  return {
60
86
  removed: Object.values(removed),
61
87
  added: Object.values(added)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-synced-block",
3
- "version": "4.2.15",
3
+ "version": "4.3.0",
4
4
  "description": "SyncedBlock plugin for @atlaskit/editor-core",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -49,7 +49,7 @@
49
49
  "@atlaskit/lozenge": "^13.1.0",
50
50
  "@atlaskit/modal-dialog": "^14.7.0",
51
51
  "@atlaskit/primitives": "^16.2.0",
52
- "@atlaskit/tokens": "8.1.0",
52
+ "@atlaskit/tokens": "8.2.0",
53
53
  "@atlaskit/tooltip": "^20.10.0",
54
54
  "@atlaskit/visually-hidden": "^3.0.0",
55
55
  "@babel/runtime": "^7.0.0",