@atlaskit/editor-plugin-show-diff 0.1.6 → 0.2.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,20 @@
1
1
  # @atlaskit/editor-plugin-show-diff
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`3df4a57528050`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/3df4a57528050) -
8
+ Update editor showDiffPlugin to take in params for preset use in Confluence version history.
9
+
10
+ ## 0.1.7
11
+
12
+ ### Patch Changes
13
+
14
+ - [`06722cb00f629`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/06722cb00f629) -
15
+ [EDITOR-1358] Remove extra parameters parased in and refactored initialisation for show-diff
16
+ editorView
17
+
3
18
  ## 0.1.6
4
19
 
5
20
  ### Patch Changes
@@ -24,4 +24,4 @@
24
24
  "path": "../../editor-common/afm-cc/tsconfig.json"
25
25
  }
26
26
  ]
27
- }
27
+ }
@@ -46,29 +46,48 @@ function isEditorViewWithNodeViews(view) {
46
46
  */
47
47
  var NodeViewSerializer = exports.NodeViewSerializer = /*#__PURE__*/function () {
48
48
  function NodeViewSerializer(params) {
49
- var _ref, _params$nodeViews, _this$editorView, _params$blocklist;
49
+ var _params$blocklist;
50
50
  (0, _classCallCheck2.default)(this, NodeViewSerializer);
51
- this.serializer = _model.DOMSerializer.fromSchema(params.schema);
52
- if (params.editorView && isEditorViewWithNodeViews(params.editorView)) {
53
- this.editorView = params.editorView;
51
+ if (params !== null && params !== void 0 && params.editorView) {
52
+ this.init({
53
+ editorView: params.editorView
54
+ });
54
55
  }
55
- this.nodeViews = (_ref = (_params$nodeViews = params.nodeViews) !== null && _params$nodeViews !== void 0 ? _params$nodeViews : (_this$editorView = this.editorView) === null || _this$editorView === void 0 ? void 0 : _this$editorView.nodeViews) !== null && _ref !== void 0 ? _ref : {};
56
56
  this.nodeViewBlocklist = new Set((_params$blocklist = params.blocklist) !== null && _params$blocklist !== void 0 ? _params$blocklist : ['tableRow', 'table', 'paragraph']);
57
57
  }
58
58
 
59
59
  /**
60
- * Attempts to create a node view for the given node.
61
- *
62
- * Returns `null` when there is no `EditorView`, no constructor for the node type,
63
- * or the node type is blocklisted. Otherwise returns the constructed node view instance.
60
+ * Initializes or reinitializes the NodeViewSerializer with a new EditorView.
61
+ * This allows the same serializer instance to be reused across different editor states.
64
62
  */
65
63
  return (0, _createClass2.default)(NodeViewSerializer, [{
64
+ key: "init",
65
+ value: function init(params) {
66
+ var _params$editorView, _ref, _this$editorView;
67
+ this.serializer = _model.DOMSerializer.fromSchema(params.editorView.state.schema);
68
+ if (isEditorViewWithNodeViews(params.editorView)) {
69
+ this.editorView = params.editorView;
70
+ }
71
+ var nodeViews =
72
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
+ ((_params$editorView = params.editorView) === null || _params$editorView === void 0 ? void 0 : _params$editorView.nodeViews) || {};
74
+ this.nodeViews = (_ref = nodeViews !== null && nodeViews !== void 0 ? nodeViews : (_this$editorView = this.editorView) === null || _this$editorView === void 0 ? void 0 : _this$editorView.nodeViews) !== null && _ref !== void 0 ? _ref : {};
75
+ }
76
+
77
+ /**
78
+ * Attempts to create a node view for the given node.
79
+ *
80
+ * Returns `null` when there is no `EditorView`, no constructor for the node type,
81
+ * or the node type is blocklisted. Otherwise returns the constructed node view instance.
82
+ */
83
+ }, {
66
84
  key: "tryCreateNodeView",
67
85
  value: function tryCreateNodeView(targetNode) {
86
+ var _this$nodeViews;
68
87
  if (!this.editorView) {
69
88
  return null;
70
89
  }
71
- var constructor = this.nodeViews[targetNode.type.name];
90
+ var constructor = (_this$nodeViews = this.nodeViews) === null || _this$nodeViews === void 0 ? void 0 : _this$nodeViews[targetNode.type.name];
72
91
  if (!constructor) {
73
92
  return null;
74
93
  }
@@ -86,6 +105,9 @@ var NodeViewSerializer = exports.NodeViewSerializer = /*#__PURE__*/function () {
86
105
  }, {
87
106
  key: "serializeNode",
88
107
  value: function serializeNode(node) {
108
+ if (!this.serializer) {
109
+ throw new Error('NodeViewSerializer must be initialized with init() before use');
110
+ }
89
111
  return this.serializer.serializeNode(node);
90
112
  }
91
113
 
@@ -95,6 +117,9 @@ var NodeViewSerializer = exports.NodeViewSerializer = /*#__PURE__*/function () {
95
117
  }, {
96
118
  key: "serializeFragment",
97
119
  value: function serializeFragment(fragment) {
120
+ if (!this.serializer) {
121
+ throw new Error('NodeViewSerializer must be initialized with init() before use');
122
+ }
98
123
  return this.serializer.serializeFragment(fragment);
99
124
  }
100
125
  }]);
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.createInlineChangedDecoration = exports.createDeletedContentDecoration = void 0;
7
7
  var _lazyNodeView = require("@atlaskit/editor-common/lazy-node-view");
8
8
  var _view = require("@atlaskit/editor-prosemirror/view");
9
- var _NodeViewSerializer = require("./NodeViewSerializer");
10
9
  var style = (0, _lazyNodeView.convertToInlineCss)({
11
10
  background: "var(--ds-background-accent-purple-subtlest, #F3F0FF)",
12
11
  textDecoration: 'underline',
@@ -53,9 +52,7 @@ var deletedContentStyleUnbounded = (0, _lazyNodeView.convertToInlineCss)({
53
52
  var createDeletedContentDecoration = exports.createDeletedContentDecoration = function createDeletedContentDecoration(_ref) {
54
53
  var change = _ref.change,
55
54
  doc = _ref.doc,
56
- tr = _ref.tr,
57
- editorView = _ref.editorView,
58
- nodeViews = _ref.nodeViews;
55
+ nodeViewSerializer = _ref.nodeViewSerializer;
59
56
  var dom = document.createElement('span');
60
57
  dom.setAttribute('style', deletedContentStyle);
61
58
 
@@ -65,11 +62,7 @@ var createDeletedContentDecoration = exports.createDeletedContentDecoration = fu
65
62
  * or sliced End depth is and match only the content and not with the entire node.
66
63
  */
67
64
  var slice = doc.slice(change.fromA, change.toA);
68
- var nodeViewSerializer = new _NodeViewSerializer.NodeViewSerializer({
69
- schema: tr.doc.type.schema,
70
- editorView: editorView,
71
- nodeViews: nodeViews
72
- });
65
+ var serializer = nodeViewSerializer;
73
66
  slice.content.forEach(function (node) {
74
67
  // Create a wrapper for each node with strikethrough
75
68
  var createWrapperWithStrikethrough = function createWrapperWithStrikethrough() {
@@ -86,7 +79,7 @@ var createDeletedContentDecoration = exports.createDeletedContentDecoration = fu
86
79
  var handleMultipleChildNodes = function handleMultipleChildNodes(node) {
87
80
  if (node.content.childCount > 1 && node.type.inlineContent) {
88
81
  node.content.forEach(function (childNode) {
89
- var childNodeView = nodeViewSerializer.tryCreateNodeView(childNode);
82
+ var childNodeView = serializer.tryCreateNodeView(childNode);
90
83
  if (childNodeView) {
91
84
  var lineBreak = document.createElement('br');
92
85
  targetNode = node;
@@ -96,7 +89,7 @@ var createDeletedContentDecoration = exports.createDeletedContentDecoration = fu
96
89
  dom.append(wrapper);
97
90
  } else {
98
91
  // Fallback to serializing the individual child node
99
- var serializedChild = nodeViewSerializer.serializeNode(childNode);
92
+ var serializedChild = serializer.serializeNode(childNode);
100
93
  dom.append(serializedChild);
101
94
  }
102
95
  });
@@ -117,7 +110,7 @@ var createDeletedContentDecoration = exports.createDeletedContentDecoration = fu
117
110
  }
118
111
  targetNode = node.content.content[0];
119
112
  fallbackSerialization = function fallbackSerialization() {
120
- return nodeViewSerializer.serializeFragment(node.content);
113
+ return serializer.serializeFragment(node.content);
121
114
  };
122
115
  } else if (isLast && slice.content.childCount === 2) {
123
116
  if (handleMultipleChildNodes(node)) {
@@ -131,9 +124,9 @@ var createDeletedContentDecoration = exports.createDeletedContentDecoration = fu
131
124
  if (node.type.name === 'paragraph') {
132
125
  var lineBreak = document.createElement('br');
133
126
  dom.append(lineBreak);
134
- return nodeViewSerializer.serializeFragment(node.content);
127
+ return serializer.serializeFragment(node.content);
135
128
  }
136
- return nodeViewSerializer.serializeFragment(node.content);
129
+ return serializer.serializeFragment(node.content);
137
130
  };
138
131
  } else {
139
132
  if (handleMultipleChildNodes(node)) {
@@ -141,12 +134,12 @@ var createDeletedContentDecoration = exports.createDeletedContentDecoration = fu
141
134
  }
142
135
  targetNode = node.content.content[0] || node;
143
136
  fallbackSerialization = function fallbackSerialization() {
144
- return nodeViewSerializer.serializeNode(node);
137
+ return serializer.serializeNode(node);
145
138
  };
146
139
  }
147
140
 
148
141
  // Try to create node view, fallback to serialization
149
- var nodeView = nodeViewSerializer.tryCreateNodeView(targetNode);
142
+ var nodeView = serializer.tryCreateNodeView(targetNode);
150
143
  if (nodeView) {
151
144
  var wrapper = createWrapperWithStrikethrough();
152
145
  wrapper.append(nodeView.dom);
@@ -14,6 +14,7 @@ var _transform = require("@atlaskit/editor-prosemirror/transform");
14
14
  var _view = require("@atlaskit/editor-prosemirror/view");
15
15
  var _decorations = require("./decorations");
16
16
  var _markDecorations = require("./markDecorations");
17
+ var _NodeViewSerializer = require("./NodeViewSerializer");
17
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; }
18
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; }
19
20
  function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
@@ -22,8 +23,7 @@ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length)
22
23
  var calculateDecorations = function calculateDecorations(_ref) {
23
24
  var state = _ref.state,
24
25
  pluginState = _ref.pluginState,
25
- editorView = _ref.editorView,
26
- nodeViews = _ref.nodeViews;
26
+ nodeViewSerializer = _ref.nodeViewSerializer;
27
27
  var originalDoc = pluginState.originalDoc,
28
28
  steps = pluginState.steps;
29
29
  if (!originalDoc || !pluginState.isDisplayingChanges) {
@@ -61,9 +61,7 @@ var calculateDecorations = function calculateDecorations(_ref) {
61
61
  decorations.push((0, _decorations.createDeletedContentDecoration)({
62
62
  change: change,
63
63
  doc: originalDoc,
64
- tr: tr,
65
- editorView: editorView,
66
- nodeViews: nodeViews
64
+ nodeViewSerializer: nodeViewSerializer
67
65
  }));
68
66
  }
69
67
  });
@@ -74,9 +72,11 @@ var calculateDecorations = function calculateDecorations(_ref) {
74
72
  };
75
73
  var showDiffPluginKey = exports.showDiffPluginKey = new _state.PluginKey('showDiffPlugin');
76
74
  var createPlugin = exports.createPlugin = function createPlugin(config) {
77
- var editorView;
78
- var setEditorView = function setEditorView(newEditorView) {
79
- editorView = newEditorView;
75
+ var nodeViewSerializer = new _NodeViewSerializer.NodeViewSerializer({});
76
+ var setNodeViewSerializer = function setNodeViewSerializer(editorView) {
77
+ nodeViewSerializer.init({
78
+ editorView: editorView
79
+ });
80
80
  };
81
81
  return new _safePlugin.SafePlugin({
82
82
  key: showDiffPluginKey,
@@ -91,12 +91,8 @@ var createPlugin = exports.createPlugin = function createPlugin(config) {
91
91
  };
92
92
  },
93
93
  apply: function apply(tr, currentPluginState, oldState, newState) {
94
- var _editorView;
95
94
  var meta = tr.getMeta(showDiffPluginKey);
96
95
  var newPluginState = currentPluginState;
97
-
98
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
- var nodeViews = ((_editorView = editorView) === null || _editorView === void 0 ? void 0 : _editorView.nodeViews) || {};
100
96
  if (meta) {
101
97
  if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'SHOW_DIFF') {
102
98
  // Update the plugin state with the new metadata
@@ -107,8 +103,7 @@ var createPlugin = exports.createPlugin = function createPlugin(config) {
107
103
  var decorations = calculateDecorations({
108
104
  state: newState,
109
105
  pluginState: newPluginState,
110
- editorView: editorView,
111
- nodeViews: nodeViews
106
+ nodeViewSerializer: nodeViewSerializer
112
107
  });
113
108
  // Update the decorations
114
109
  newPluginState.decorations = decorations;
@@ -127,7 +122,7 @@ var createPlugin = exports.createPlugin = function createPlugin(config) {
127
122
  }
128
123
  },
129
124
  view: function view(editorView) {
130
- setEditorView(editorView);
125
+ setNodeViewSerializer(editorView);
131
126
  var isFirst = true;
132
127
  return {
133
128
  update: function update(view) {
@@ -37,15 +37,31 @@ export function isEditorViewWithNodeViews(view) {
37
37
  */
38
38
  export class NodeViewSerializer {
39
39
  constructor(params) {
40
- var _ref, _params$nodeViews, _this$editorView, _params$blocklist;
41
- this.serializer = DOMSerializer.fromSchema(params.schema);
42
- if (params.editorView && isEditorViewWithNodeViews(params.editorView)) {
43
- this.editorView = params.editorView;
40
+ var _params$blocklist;
41
+ if (params !== null && params !== void 0 && params.editorView) {
42
+ this.init({
43
+ editorView: params.editorView
44
+ });
44
45
  }
45
- this.nodeViews = (_ref = (_params$nodeViews = params.nodeViews) !== null && _params$nodeViews !== void 0 ? _params$nodeViews : (_this$editorView = this.editorView) === null || _this$editorView === void 0 ? void 0 : _this$editorView.nodeViews) !== null && _ref !== void 0 ? _ref : {};
46
46
  this.nodeViewBlocklist = new Set((_params$blocklist = params.blocklist) !== null && _params$blocklist !== void 0 ? _params$blocklist : ['tableRow', 'table', 'paragraph']);
47
47
  }
48
48
 
49
+ /**
50
+ * Initializes or reinitializes the NodeViewSerializer with a new EditorView.
51
+ * This allows the same serializer instance to be reused across different editor states.
52
+ */
53
+ init(params) {
54
+ var _params$editorView, _ref, _this$editorView;
55
+ this.serializer = DOMSerializer.fromSchema(params.editorView.state.schema);
56
+ if (isEditorViewWithNodeViews(params.editorView)) {
57
+ this.editorView = params.editorView;
58
+ }
59
+ const nodeViews =
60
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
+ ((_params$editorView = params.editorView) === null || _params$editorView === void 0 ? void 0 : _params$editorView.nodeViews) || {};
62
+ this.nodeViews = (_ref = nodeViews !== null && nodeViews !== void 0 ? nodeViews : (_this$editorView = this.editorView) === null || _this$editorView === void 0 ? void 0 : _this$editorView.nodeViews) !== null && _ref !== void 0 ? _ref : {};
63
+ }
64
+
49
65
  /**
50
66
  * Attempts to create a node view for the given node.
51
67
  *
@@ -53,10 +69,11 @@ export class NodeViewSerializer {
53
69
  * or the node type is blocklisted. Otherwise returns the constructed node view instance.
54
70
  */
55
71
  tryCreateNodeView(targetNode) {
72
+ var _this$nodeViews;
56
73
  if (!this.editorView) {
57
74
  return null;
58
75
  }
59
- const constructor = this.nodeViews[targetNode.type.name];
76
+ const constructor = (_this$nodeViews = this.nodeViews) === null || _this$nodeViews === void 0 ? void 0 : _this$nodeViews[targetNode.type.name];
60
77
  if (!constructor) {
61
78
  return null;
62
79
  }
@@ -70,6 +87,9 @@ export class NodeViewSerializer {
70
87
  * Serializes a node to a DOM `Node` using the schema's `DOMSerializer`.
71
88
  */
72
89
  serializeNode(node) {
90
+ if (!this.serializer) {
91
+ throw new Error('NodeViewSerializer must be initialized with init() before use');
92
+ }
73
93
  return this.serializer.serializeNode(node);
74
94
  }
75
95
 
@@ -77,6 +97,9 @@ export class NodeViewSerializer {
77
97
  * Serializes a fragment to a `DocumentFragment` using the schema's `DOMSerializer`.
78
98
  */
79
99
  serializeFragment(fragment) {
100
+ if (!this.serializer) {
101
+ throw new Error('NodeViewSerializer must be initialized with init() before use');
102
+ }
80
103
  return this.serializer.serializeFragment(fragment);
81
104
  }
82
105
  }
@@ -1,6 +1,5 @@
1
1
  import { convertToInlineCss } from '@atlaskit/editor-common/lazy-node-view';
2
2
  import { Decoration } from '@atlaskit/editor-prosemirror/view';
3
- import { NodeViewSerializer } from './NodeViewSerializer';
4
3
  const style = convertToInlineCss({
5
4
  background: "var(--ds-background-accent-purple-subtlest, #F3F0FF)",
6
5
  textDecoration: 'underline',
@@ -45,9 +44,7 @@ const deletedContentStyleUnbounded = convertToInlineCss({
45
44
  export const createDeletedContentDecoration = ({
46
45
  change,
47
46
  doc,
48
- tr,
49
- editorView,
50
- nodeViews
47
+ nodeViewSerializer
51
48
  }) => {
52
49
  const dom = document.createElement('span');
53
50
  dom.setAttribute('style', deletedContentStyle);
@@ -58,11 +55,7 @@ export const createDeletedContentDecoration = ({
58
55
  * or sliced End depth is and match only the content and not with the entire node.
59
56
  */
60
57
  const slice = doc.slice(change.fromA, change.toA);
61
- const nodeViewSerializer = new NodeViewSerializer({
62
- schema: tr.doc.type.schema,
63
- editorView,
64
- nodeViews
65
- });
58
+ const serializer = nodeViewSerializer;
66
59
  slice.content.forEach(node => {
67
60
  // Create a wrapper for each node with strikethrough
68
61
  const createWrapperWithStrikethrough = () => {
@@ -79,7 +72,7 @@ export const createDeletedContentDecoration = ({
79
72
  const handleMultipleChildNodes = node => {
80
73
  if (node.content.childCount > 1 && node.type.inlineContent) {
81
74
  node.content.forEach(childNode => {
82
- const childNodeView = nodeViewSerializer.tryCreateNodeView(childNode);
75
+ const childNodeView = serializer.tryCreateNodeView(childNode);
83
76
  if (childNodeView) {
84
77
  const lineBreak = document.createElement('br');
85
78
  targetNode = node;
@@ -89,7 +82,7 @@ export const createDeletedContentDecoration = ({
89
82
  dom.append(wrapper);
90
83
  } else {
91
84
  // Fallback to serializing the individual child node
92
- const serializedChild = nodeViewSerializer.serializeNode(childNode);
85
+ const serializedChild = serializer.serializeNode(childNode);
93
86
  dom.append(serializedChild);
94
87
  }
95
88
  });
@@ -109,7 +102,7 @@ export const createDeletedContentDecoration = ({
109
102
  return;
110
103
  }
111
104
  targetNode = node.content.content[0];
112
- fallbackSerialization = () => nodeViewSerializer.serializeFragment(node.content);
105
+ fallbackSerialization = () => serializer.serializeFragment(node.content);
113
106
  } else if (isLast && slice.content.childCount === 2) {
114
107
  if (handleMultipleChildNodes(node)) {
115
108
  return;
@@ -122,20 +115,20 @@ export const createDeletedContentDecoration = ({
122
115
  if (node.type.name === 'paragraph') {
123
116
  const lineBreak = document.createElement('br');
124
117
  dom.append(lineBreak);
125
- return nodeViewSerializer.serializeFragment(node.content);
118
+ return serializer.serializeFragment(node.content);
126
119
  }
127
- return nodeViewSerializer.serializeFragment(node.content);
120
+ return serializer.serializeFragment(node.content);
128
121
  };
129
122
  } else {
130
123
  if (handleMultipleChildNodes(node)) {
131
124
  return;
132
125
  }
133
126
  targetNode = node.content.content[0] || node;
134
- fallbackSerialization = () => nodeViewSerializer.serializeNode(node);
127
+ fallbackSerialization = () => serializer.serializeNode(node);
135
128
  }
136
129
 
137
130
  // Try to create node view, fallback to serialization
138
- const nodeView = nodeViewSerializer.tryCreateNodeView(targetNode);
131
+ const nodeView = serializer.tryCreateNodeView(targetNode);
139
132
  if (nodeView) {
140
133
  const wrapper = createWrapperWithStrikethrough();
141
134
  wrapper.append(nodeView.dom);
@@ -7,11 +7,11 @@ import { Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform'
7
7
  import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
8
8
  import { createInlineChangedDecoration, createDeletedContentDecoration } from './decorations';
9
9
  import { getMarkChangeRanges } from './markDecorations';
10
+ import { NodeViewSerializer } from './NodeViewSerializer';
10
11
  const calculateDecorations = ({
11
12
  state,
12
13
  pluginState,
13
- editorView,
14
- nodeViews
14
+ nodeViewSerializer
15
15
  }) => {
16
16
  const {
17
17
  originalDoc,
@@ -45,9 +45,7 @@ const calculateDecorations = ({
45
45
  decorations.push(createDeletedContentDecoration({
46
46
  change,
47
47
  doc: originalDoc,
48
- tr,
49
- editorView,
50
- nodeViews
48
+ nodeViewSerializer
51
49
  }));
52
50
  }
53
51
  });
@@ -58,9 +56,11 @@ const calculateDecorations = ({
58
56
  };
59
57
  export const showDiffPluginKey = new PluginKey('showDiffPlugin');
60
58
  export const createPlugin = config => {
61
- let editorView;
62
- const setEditorView = newEditorView => {
63
- editorView = newEditorView;
59
+ const nodeViewSerializer = new NodeViewSerializer({});
60
+ const setNodeViewSerializer = editorView => {
61
+ nodeViewSerializer.init({
62
+ editorView
63
+ });
64
64
  };
65
65
  return new SafePlugin({
66
66
  key: showDiffPluginKey,
@@ -75,12 +75,8 @@ export const createPlugin = config => {
75
75
  };
76
76
  },
77
77
  apply: (tr, currentPluginState, oldState, newState) => {
78
- var _editorView;
79
78
  const meta = tr.getMeta(showDiffPluginKey);
80
79
  let newPluginState = currentPluginState;
81
-
82
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
- const nodeViews = ((_editorView = editorView) === null || _editorView === void 0 ? void 0 : _editorView.nodeViews) || {};
84
80
  if (meta) {
85
81
  if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'SHOW_DIFF') {
86
82
  // Update the plugin state with the new metadata
@@ -93,8 +89,7 @@ export const createPlugin = config => {
93
89
  const decorations = calculateDecorations({
94
90
  state: newState,
95
91
  pluginState: newPluginState,
96
- editorView,
97
- nodeViews: nodeViews
92
+ nodeViewSerializer
98
93
  });
99
94
  // Update the decorations
100
95
  newPluginState.decorations = decorations;
@@ -119,7 +114,7 @@ export const createPlugin = config => {
119
114
  }
120
115
  },
121
116
  view(editorView) {
122
- setEditorView(editorView);
117
+ setNodeViewSerializer(editorView);
123
118
  let isFirst = true;
124
119
  return {
125
120
  update(view) {
@@ -39,29 +39,48 @@ export function isEditorViewWithNodeViews(view) {
39
39
  */
40
40
  export var NodeViewSerializer = /*#__PURE__*/function () {
41
41
  function NodeViewSerializer(params) {
42
- var _ref, _params$nodeViews, _this$editorView, _params$blocklist;
42
+ var _params$blocklist;
43
43
  _classCallCheck(this, NodeViewSerializer);
44
- this.serializer = DOMSerializer.fromSchema(params.schema);
45
- if (params.editorView && isEditorViewWithNodeViews(params.editorView)) {
46
- this.editorView = params.editorView;
44
+ if (params !== null && params !== void 0 && params.editorView) {
45
+ this.init({
46
+ editorView: params.editorView
47
+ });
47
48
  }
48
- this.nodeViews = (_ref = (_params$nodeViews = params.nodeViews) !== null && _params$nodeViews !== void 0 ? _params$nodeViews : (_this$editorView = this.editorView) === null || _this$editorView === void 0 ? void 0 : _this$editorView.nodeViews) !== null && _ref !== void 0 ? _ref : {};
49
49
  this.nodeViewBlocklist = new Set((_params$blocklist = params.blocklist) !== null && _params$blocklist !== void 0 ? _params$blocklist : ['tableRow', 'table', 'paragraph']);
50
50
  }
51
51
 
52
52
  /**
53
- * Attempts to create a node view for the given node.
54
- *
55
- * Returns `null` when there is no `EditorView`, no constructor for the node type,
56
- * or the node type is blocklisted. Otherwise returns the constructed node view instance.
53
+ * Initializes or reinitializes the NodeViewSerializer with a new EditorView.
54
+ * This allows the same serializer instance to be reused across different editor states.
57
55
  */
58
56
  return _createClass(NodeViewSerializer, [{
57
+ key: "init",
58
+ value: function init(params) {
59
+ var _params$editorView, _ref, _this$editorView;
60
+ this.serializer = DOMSerializer.fromSchema(params.editorView.state.schema);
61
+ if (isEditorViewWithNodeViews(params.editorView)) {
62
+ this.editorView = params.editorView;
63
+ }
64
+ var nodeViews =
65
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
+ ((_params$editorView = params.editorView) === null || _params$editorView === void 0 ? void 0 : _params$editorView.nodeViews) || {};
67
+ this.nodeViews = (_ref = nodeViews !== null && nodeViews !== void 0 ? nodeViews : (_this$editorView = this.editorView) === null || _this$editorView === void 0 ? void 0 : _this$editorView.nodeViews) !== null && _ref !== void 0 ? _ref : {};
68
+ }
69
+
70
+ /**
71
+ * Attempts to create a node view for the given node.
72
+ *
73
+ * Returns `null` when there is no `EditorView`, no constructor for the node type,
74
+ * or the node type is blocklisted. Otherwise returns the constructed node view instance.
75
+ */
76
+ }, {
59
77
  key: "tryCreateNodeView",
60
78
  value: function tryCreateNodeView(targetNode) {
79
+ var _this$nodeViews;
61
80
  if (!this.editorView) {
62
81
  return null;
63
82
  }
64
- var constructor = this.nodeViews[targetNode.type.name];
83
+ var constructor = (_this$nodeViews = this.nodeViews) === null || _this$nodeViews === void 0 ? void 0 : _this$nodeViews[targetNode.type.name];
65
84
  if (!constructor) {
66
85
  return null;
67
86
  }
@@ -79,6 +98,9 @@ export var NodeViewSerializer = /*#__PURE__*/function () {
79
98
  }, {
80
99
  key: "serializeNode",
81
100
  value: function serializeNode(node) {
101
+ if (!this.serializer) {
102
+ throw new Error('NodeViewSerializer must be initialized with init() before use');
103
+ }
82
104
  return this.serializer.serializeNode(node);
83
105
  }
84
106
 
@@ -88,6 +110,9 @@ export var NodeViewSerializer = /*#__PURE__*/function () {
88
110
  }, {
89
111
  key: "serializeFragment",
90
112
  value: function serializeFragment(fragment) {
113
+ if (!this.serializer) {
114
+ throw new Error('NodeViewSerializer must be initialized with init() before use');
115
+ }
91
116
  return this.serializer.serializeFragment(fragment);
92
117
  }
93
118
  }]);
@@ -1,6 +1,5 @@
1
1
  import { convertToInlineCss } from '@atlaskit/editor-common/lazy-node-view';
2
2
  import { Decoration } from '@atlaskit/editor-prosemirror/view';
3
- import { NodeViewSerializer } from './NodeViewSerializer';
4
3
  var style = convertToInlineCss({
5
4
  background: "var(--ds-background-accent-purple-subtlest, #F3F0FF)",
6
5
  textDecoration: 'underline',
@@ -47,9 +46,7 @@ var deletedContentStyleUnbounded = convertToInlineCss({
47
46
  export var createDeletedContentDecoration = function createDeletedContentDecoration(_ref) {
48
47
  var change = _ref.change,
49
48
  doc = _ref.doc,
50
- tr = _ref.tr,
51
- editorView = _ref.editorView,
52
- nodeViews = _ref.nodeViews;
49
+ nodeViewSerializer = _ref.nodeViewSerializer;
53
50
  var dom = document.createElement('span');
54
51
  dom.setAttribute('style', deletedContentStyle);
55
52
 
@@ -59,11 +56,7 @@ export var createDeletedContentDecoration = function createDeletedContentDecorat
59
56
  * or sliced End depth is and match only the content and not with the entire node.
60
57
  */
61
58
  var slice = doc.slice(change.fromA, change.toA);
62
- var nodeViewSerializer = new NodeViewSerializer({
63
- schema: tr.doc.type.schema,
64
- editorView: editorView,
65
- nodeViews: nodeViews
66
- });
59
+ var serializer = nodeViewSerializer;
67
60
  slice.content.forEach(function (node) {
68
61
  // Create a wrapper for each node with strikethrough
69
62
  var createWrapperWithStrikethrough = function createWrapperWithStrikethrough() {
@@ -80,7 +73,7 @@ export var createDeletedContentDecoration = function createDeletedContentDecorat
80
73
  var handleMultipleChildNodes = function handleMultipleChildNodes(node) {
81
74
  if (node.content.childCount > 1 && node.type.inlineContent) {
82
75
  node.content.forEach(function (childNode) {
83
- var childNodeView = nodeViewSerializer.tryCreateNodeView(childNode);
76
+ var childNodeView = serializer.tryCreateNodeView(childNode);
84
77
  if (childNodeView) {
85
78
  var lineBreak = document.createElement('br');
86
79
  targetNode = node;
@@ -90,7 +83,7 @@ export var createDeletedContentDecoration = function createDeletedContentDecorat
90
83
  dom.append(wrapper);
91
84
  } else {
92
85
  // Fallback to serializing the individual child node
93
- var serializedChild = nodeViewSerializer.serializeNode(childNode);
86
+ var serializedChild = serializer.serializeNode(childNode);
94
87
  dom.append(serializedChild);
95
88
  }
96
89
  });
@@ -111,7 +104,7 @@ export var createDeletedContentDecoration = function createDeletedContentDecorat
111
104
  }
112
105
  targetNode = node.content.content[0];
113
106
  fallbackSerialization = function fallbackSerialization() {
114
- return nodeViewSerializer.serializeFragment(node.content);
107
+ return serializer.serializeFragment(node.content);
115
108
  };
116
109
  } else if (isLast && slice.content.childCount === 2) {
117
110
  if (handleMultipleChildNodes(node)) {
@@ -125,9 +118,9 @@ export var createDeletedContentDecoration = function createDeletedContentDecorat
125
118
  if (node.type.name === 'paragraph') {
126
119
  var lineBreak = document.createElement('br');
127
120
  dom.append(lineBreak);
128
- return nodeViewSerializer.serializeFragment(node.content);
121
+ return serializer.serializeFragment(node.content);
129
122
  }
130
- return nodeViewSerializer.serializeFragment(node.content);
123
+ return serializer.serializeFragment(node.content);
131
124
  };
132
125
  } else {
133
126
  if (handleMultipleChildNodes(node)) {
@@ -135,12 +128,12 @@ export var createDeletedContentDecoration = function createDeletedContentDecorat
135
128
  }
136
129
  targetNode = node.content.content[0] || node;
137
130
  fallbackSerialization = function fallbackSerialization() {
138
- return nodeViewSerializer.serializeNode(node);
131
+ return serializer.serializeNode(node);
139
132
  };
140
133
  }
141
134
 
142
135
  // Try to create node view, fallback to serialization
143
- var nodeView = nodeViewSerializer.tryCreateNodeView(targetNode);
136
+ var nodeView = serializer.tryCreateNodeView(targetNode);
144
137
  if (nodeView) {
145
138
  var wrapper = createWrapperWithStrikethrough();
146
139
  wrapper.append(nodeView.dom);
@@ -13,11 +13,11 @@ import { Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform'
13
13
  import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
14
14
  import { createInlineChangedDecoration, createDeletedContentDecoration } from './decorations';
15
15
  import { getMarkChangeRanges } from './markDecorations';
16
+ import { NodeViewSerializer } from './NodeViewSerializer';
16
17
  var calculateDecorations = function calculateDecorations(_ref) {
17
18
  var state = _ref.state,
18
19
  pluginState = _ref.pluginState,
19
- editorView = _ref.editorView,
20
- nodeViews = _ref.nodeViews;
20
+ nodeViewSerializer = _ref.nodeViewSerializer;
21
21
  var originalDoc = pluginState.originalDoc,
22
22
  steps = pluginState.steps;
23
23
  if (!originalDoc || !pluginState.isDisplayingChanges) {
@@ -55,9 +55,7 @@ var calculateDecorations = function calculateDecorations(_ref) {
55
55
  decorations.push(createDeletedContentDecoration({
56
56
  change: change,
57
57
  doc: originalDoc,
58
- tr: tr,
59
- editorView: editorView,
60
- nodeViews: nodeViews
58
+ nodeViewSerializer: nodeViewSerializer
61
59
  }));
62
60
  }
63
61
  });
@@ -68,9 +66,11 @@ var calculateDecorations = function calculateDecorations(_ref) {
68
66
  };
69
67
  export var showDiffPluginKey = new PluginKey('showDiffPlugin');
70
68
  export var createPlugin = function createPlugin(config) {
71
- var editorView;
72
- var setEditorView = function setEditorView(newEditorView) {
73
- editorView = newEditorView;
69
+ var nodeViewSerializer = new NodeViewSerializer({});
70
+ var setNodeViewSerializer = function setNodeViewSerializer(editorView) {
71
+ nodeViewSerializer.init({
72
+ editorView: editorView
73
+ });
74
74
  };
75
75
  return new SafePlugin({
76
76
  key: showDiffPluginKey,
@@ -85,12 +85,8 @@ export var createPlugin = function createPlugin(config) {
85
85
  };
86
86
  },
87
87
  apply: function apply(tr, currentPluginState, oldState, newState) {
88
- var _editorView;
89
88
  var meta = tr.getMeta(showDiffPluginKey);
90
89
  var newPluginState = currentPluginState;
91
-
92
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
- var nodeViews = ((_editorView = editorView) === null || _editorView === void 0 ? void 0 : _editorView.nodeViews) || {};
94
90
  if (meta) {
95
91
  if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'SHOW_DIFF') {
96
92
  // Update the plugin state with the new metadata
@@ -101,8 +97,7 @@ export var createPlugin = function createPlugin(config) {
101
97
  var decorations = calculateDecorations({
102
98
  state: newState,
103
99
  pluginState: newPluginState,
104
- editorView: editorView,
105
- nodeViews: nodeViews
100
+ nodeViewSerializer: nodeViewSerializer
106
101
  });
107
102
  // Update the decorations
108
103
  newPluginState.decorations = decorations;
@@ -121,7 +116,7 @@ export var createPlugin = function createPlugin(config) {
121
116
  }
122
117
  },
123
118
  view: function view(editorView) {
124
- setEditorView(editorView);
119
+ setNodeViewSerializer(editorView);
125
120
  var isFirst = true;
126
121
  return {
127
122
  update: function update(view) {
@@ -1,2 +1,2 @@
1
1
  export { showDiffPlugin } from './showDiffPlugin';
2
- export type { ShowDiffPlugin } from './showDiffPluginType';
2
+ export type { ShowDiffPlugin, DiffParams } from './showDiffPluginType';
@@ -1,5 +1,5 @@
1
1
  import { type NodeViewConstructor } from '@atlaskit/editor-common/lazy-node-view';
2
- import type { Schema, Node as PMNode, Fragment } from '@atlaskit/editor-prosemirror/model';
2
+ import type { Node as PMNode, Fragment } from '@atlaskit/editor-prosemirror/model';
3
3
  import type { EditorView } from '@atlaskit/editor-prosemirror/view';
4
4
  /**
5
5
  * Utilities for working with ProseMirror node views and DOM serialization within the
@@ -34,16 +34,21 @@ export declare function isEditorViewWithNodeViews(view: EditorView): view is Edi
34
34
  * - Preventing node view creation for blocklisted node types
35
35
  */
36
36
  export declare class NodeViewSerializer {
37
- private serializer;
38
37
  private editorView?;
39
- private nodeViews;
38
+ private serializer?;
39
+ private nodeViews?;
40
40
  private nodeViewBlocklist;
41
41
  constructor(params: {
42
42
  blocklist?: string[];
43
43
  editorView?: EditorView;
44
- nodeViews?: Record<string, NodeViewConstructor>;
45
- schema: Schema;
46
44
  });
45
+ /**
46
+ * Initializes or reinitializes the NodeViewSerializer with a new EditorView.
47
+ * This allows the same serializer instance to be reused across different editor states.
48
+ */
49
+ init(params: {
50
+ editorView: EditorView;
51
+ }): void;
47
52
  /**
48
53
  * Attempts to create a node view for the given node.
49
54
  *
@@ -1,9 +1,7 @@
1
1
  import type { Change } from 'prosemirror-changeset';
2
- import { type NodeViewConstructor } from '@atlaskit/editor-common/lazy-node-view';
3
2
  import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
4
- import type { Transaction } from '@atlaskit/editor-prosemirror/state';
5
- import type { EditorView } from '@atlaskit/editor-prosemirror/view';
6
3
  import { Decoration } from '@atlaskit/editor-prosemirror/view';
4
+ import type { NodeViewSerializer } from './NodeViewSerializer';
7
5
  /**
8
6
  * Inline decoration used for insertions as the content already exists in the document
9
7
  *
@@ -17,9 +15,7 @@ export declare const createInlineChangedDecoration: (change: {
17
15
  interface DeletedContentDecorationProps {
18
16
  change: Change;
19
17
  doc: PMNode;
20
- editorView?: EditorView;
21
- nodeViews: Record<string, NodeViewConstructor>;
22
- tr: Transaction;
18
+ nodeViewSerializer: NodeViewSerializer;
23
19
  }
24
20
  /**
25
21
  * Creates a widget to represent the deleted content in the editor
@@ -29,5 +25,5 @@ interface DeletedContentDecorationProps {
29
25
  * @param props.tr The relevant transaction this decoration is being created against
30
26
  * @returns Prosemirror widget decoration
31
27
  */
32
- export declare const createDeletedContentDecoration: ({ change, doc, tr, editorView, nodeViews, }: DeletedContentDecorationProps) => Decoration;
28
+ export declare const createDeletedContentDecoration: ({ change, doc, nodeViewSerializer, }: DeletedContentDecorationProps) => Decoration;
33
29
  export {};
@@ -1,2 +1,2 @@
1
1
  export { showDiffPlugin } from './showDiffPlugin';
2
- export type { ShowDiffPlugin } from './showDiffPluginType';
2
+ export type { ShowDiffPlugin, DiffParams } from './showDiffPluginType';
@@ -1,5 +1,5 @@
1
1
  import { type NodeViewConstructor } from '@atlaskit/editor-common/lazy-node-view';
2
- import type { Schema, Node as PMNode, Fragment } from '@atlaskit/editor-prosemirror/model';
2
+ import type { Node as PMNode, Fragment } from '@atlaskit/editor-prosemirror/model';
3
3
  import type { EditorView } from '@atlaskit/editor-prosemirror/view';
4
4
  /**
5
5
  * Utilities for working with ProseMirror node views and DOM serialization within the
@@ -34,16 +34,21 @@ export declare function isEditorViewWithNodeViews(view: EditorView): view is Edi
34
34
  * - Preventing node view creation for blocklisted node types
35
35
  */
36
36
  export declare class NodeViewSerializer {
37
- private serializer;
38
37
  private editorView?;
39
- private nodeViews;
38
+ private serializer?;
39
+ private nodeViews?;
40
40
  private nodeViewBlocklist;
41
41
  constructor(params: {
42
42
  blocklist?: string[];
43
43
  editorView?: EditorView;
44
- nodeViews?: Record<string, NodeViewConstructor>;
45
- schema: Schema;
46
44
  });
45
+ /**
46
+ * Initializes or reinitializes the NodeViewSerializer with a new EditorView.
47
+ * This allows the same serializer instance to be reused across different editor states.
48
+ */
49
+ init(params: {
50
+ editorView: EditorView;
51
+ }): void;
47
52
  /**
48
53
  * Attempts to create a node view for the given node.
49
54
  *
@@ -1,9 +1,7 @@
1
1
  import type { Change } from 'prosemirror-changeset';
2
- import { type NodeViewConstructor } from '@atlaskit/editor-common/lazy-node-view';
3
2
  import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
4
- import type { Transaction } from '@atlaskit/editor-prosemirror/state';
5
- import type { EditorView } from '@atlaskit/editor-prosemirror/view';
6
3
  import { Decoration } from '@atlaskit/editor-prosemirror/view';
4
+ import type { NodeViewSerializer } from './NodeViewSerializer';
7
5
  /**
8
6
  * Inline decoration used for insertions as the content already exists in the document
9
7
  *
@@ -17,9 +15,7 @@ export declare const createInlineChangedDecoration: (change: {
17
15
  interface DeletedContentDecorationProps {
18
16
  change: Change;
19
17
  doc: PMNode;
20
- editorView?: EditorView;
21
- nodeViews: Record<string, NodeViewConstructor>;
22
- tr: Transaction;
18
+ nodeViewSerializer: NodeViewSerializer;
23
19
  }
24
20
  /**
25
21
  * Creates a widget to represent the deleted content in the editor
@@ -29,5 +25,5 @@ interface DeletedContentDecorationProps {
29
25
  * @param props.tr The relevant transaction this decoration is being created against
30
26
  * @returns Prosemirror widget decoration
31
27
  */
32
- export declare const createDeletedContentDecoration: ({ change, doc, tr, editorView, nodeViews, }: DeletedContentDecorationProps) => Decoration;
28
+ export declare const createDeletedContentDecoration: ({ change, doc, nodeViewSerializer, }: DeletedContentDecorationProps) => Decoration;
33
29
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-show-diff",
3
- "version": "0.1.6",
3
+ "version": "0.2.0",
4
4
  "description": "ShowDiff plugin for @atlaskit/editor-core",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -27,17 +27,14 @@
27
27
  },
28
28
  "sideEffects": false,
29
29
  "atlaskit:src": "src/index.ts",
30
- "af:exports": {
31
- ".": "./src/index.ts"
32
- },
33
30
  "dependencies": {
34
31
  "@atlaskit/editor-prosemirror": "7.0.0",
35
- "@atlaskit/tokens": "^6.0.0",
32
+ "@atlaskit/tokens": "^6.1.0",
36
33
  "@babel/runtime": "^7.0.0",
37
34
  "prosemirror-changeset": "^2.2.1"
38
35
  },
39
36
  "peerDependencies": {
40
- "@atlaskit/editor-common": "^107.29.0",
37
+ "@atlaskit/editor-common": "^107.33.0",
41
38
  "react": "^18.2.0"
42
39
  },
43
40
  "techstack": {