@atlaskit/editor-plugin-track-changes 2.0.0 → 2.1.1

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,22 @@
1
1
  # @atlaskit/editor-plugin-track-changes
2
2
 
3
+ ## 2.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#190819](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/190819)
8
+ [`20420774e83cc`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/20420774e83cc) -
9
+ Include Track chages plugin
10
+
11
+ ## 2.1.0
12
+
13
+ ### Minor Changes
14
+
15
+ - [#189258](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/189258)
16
+ [`e6411aa283a9e`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/e6411aa283a9e) -
17
+ Exposes new state to track if the diff plugin is available for use (ie. for external buttons).
18
+ Defaults to false and is set to true when changes are available.
19
+
3
20
  ## 2.0.0
4
21
 
5
22
  ### Major Changes
@@ -0,0 +1,27 @@
1
+ {
2
+ "extends": "../../../../tsconfig.entry-points.confluence.json",
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "composite": true,
6
+ "outDir": "../../../../../confluence/tsDist/@atlaskit__editor-plugin-track-changes",
7
+ "rootDir": "../"
8
+ },
9
+ "include": [
10
+ "../src/**/*.ts",
11
+ "../src/**/*.tsx"
12
+ ],
13
+ "exclude": [
14
+ "../src/**/__tests__/*",
15
+ "../src/**/*.test.*",
16
+ "../src/**/test.*",
17
+ "../src/**/examples.*"
18
+ ],
19
+ "references": [
20
+ {
21
+ "path": "../../../design-system/tokens/afm-cc/tsconfig.json"
22
+ },
23
+ {
24
+ "path": "../../editor-common/afm-cc/tsconfig.json"
25
+ }
26
+ ]
27
+ }
@@ -0,0 +1,27 @@
1
+ {
2
+ "extends": "../../../../tsconfig.entry-points.jira.json",
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "outDir": "../../../../../tsDist/@atlaskit__editor-plugin-track-changes/app",
6
+ "rootDir": "../",
7
+ "composite": true
8
+ },
9
+ "include": [
10
+ "../src/**/*.ts",
11
+ "../src/**/*.tsx"
12
+ ],
13
+ "exclude": [
14
+ "../src/**/__tests__/*",
15
+ "../src/**/*.test.*",
16
+ "../src/**/test.*",
17
+ "../src/**/examples.*"
18
+ ],
19
+ "references": [
20
+ {
21
+ "path": "../../../design-system/tokens/afm-jira/tsconfig.json"
22
+ },
23
+ {
24
+ "path": "../../editor-common/afm-jira/tsconfig.json"
25
+ }
26
+ ]
27
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createInlineChangedDecoration = exports.createDeletedContentDecoration = void 0;
7
+ var _lazyNodeView = require("@atlaskit/editor-common/lazy-node-view");
8
+ var _model = require("@atlaskit/editor-prosemirror/model");
9
+ var _view = require("@atlaskit/editor-prosemirror/view");
10
+ var style = (0, _lazyNodeView.convertToInlineCss)({
11
+ background: "var(--ds-background-accent-purple-subtlest, #F3F0FF)",
12
+ textDecoration: 'underline',
13
+ textDecorationStyle: 'dotted',
14
+ textDecorationThickness: "var(--ds-space-025, 2px)",
15
+ textDecorationColor: "var(--ds-border-accent-purple, #8270DB)"
16
+ });
17
+
18
+ /**
19
+ * Inline decoration used for insertions as the content already exists in the document
20
+ *
21
+ * @param change Changeset "change" containing information about the change content + range
22
+ * @returns Prosemirror inline decoration
23
+ */
24
+ var createInlineChangedDecoration = exports.createInlineChangedDecoration = function createInlineChangedDecoration(change) {
25
+ return _view.Decoration.inline(change.fromB, change.toB, {
26
+ style: style
27
+ }, {});
28
+ };
29
+ var deletedContentStyle = (0, _lazyNodeView.convertToInlineCss)({
30
+ color: "var(--ds-text-accent-gray, #44546F)",
31
+ textDecoration: 'line-through'
32
+ });
33
+
34
+ /**
35
+ * Creates a widget to represent the deleted content in the editor
36
+ *
37
+ * @param props.change Changeset "change" containing information about the change content + range
38
+ * @param props.doc Baseline doc to compare against
39
+ * @param props.tr The relevant transaction this decoration is being created against
40
+ * @returns Prosemirror widget decoration
41
+ */
42
+ var createDeletedContentDecoration = exports.createDeletedContentDecoration = function createDeletedContentDecoration(_ref) {
43
+ var change = _ref.change,
44
+ doc = _ref.doc,
45
+ tr = _ref.tr;
46
+ var dom = document.createElement('span');
47
+ dom.setAttribute('style', deletedContentStyle);
48
+ dom.appendChild(_model.DOMSerializer.fromSchema(tr.doc.type.schema).serializeFragment(doc.slice(change.fromA, change.toA).content));
49
+
50
+ // Widget decoration used for deletions as the content is not in the document
51
+ // and we want to display the deleted content with a style.
52
+ return _view.Decoration.widget(change.fromB, dom, {
53
+ marks: []
54
+ });
55
+ };
@@ -7,16 +7,18 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.trackChangesPluginKey = exports.createTrackChangesPlugin = void 0;
8
8
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
9
  var _prosemirrorChangeset = require("prosemirror-changeset");
10
- var _lazyNodeView = require("@atlaskit/editor-common/lazy-node-view");
11
10
  var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
12
- var _model = require("@atlaskit/editor-prosemirror/model");
13
11
  var _state = require("@atlaskit/editor-prosemirror/state");
12
+ var _transform = require("@atlaskit/editor-prosemirror/transform");
14
13
  var _view = require("@atlaskit/editor-prosemirror/view");
14
+ var _decorations = require("./decorations");
15
15
  var _types = require("./types");
16
16
  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; }
17
17
  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; }
18
18
  var trackChangesPluginKey = exports.trackChangesPluginKey = new _state.PluginKey('trackChangesPlugin');
19
19
  var createTrackChangesPlugin = exports.createTrackChangesPlugin = function createTrackChangesPlugin() {
20
+ // Mark the state to be reset on next time the document has a meaningful change
21
+ var resetBaseline = false;
20
22
  return new _safePlugin.SafePlugin({
21
23
  key: trackChangesPluginKey,
22
24
  state: {
@@ -24,84 +26,73 @@ var createTrackChangesPlugin = exports.createTrackChangesPlugin = function creat
24
26
  var doc = _ref.doc;
25
27
  return {
26
28
  changes: _prosemirrorChangeset.ChangeSet.create(doc),
27
- shouldChangesBeDispalyed: false,
29
+ shouldChangesBeDisplayed: false,
30
+ isShowDiffAvailable: false,
28
31
  baselineDoc: doc,
29
32
  numOfChanges: 0
30
33
  };
31
34
  },
32
35
  apply: function apply(tr, state, oldState, newState) {
33
36
  var metadata = tr.getMeta(trackChangesPluginKey);
34
- if (metadata) {
35
- if (metadata.action === _types.TOGGLE_TRACK_CHANGES_ACTION.TOGGLE_TRACK_CHANGES) {
36
- return _objectSpread(_objectSpread({}, state), {}, {
37
- baselineDoc: state.shouldChangesBeDispalyed ? tr.doc : state.baselineDoc,
38
- shouldChangesBeDispalyed: !state.shouldChangesBeDispalyed,
39
- changes: state.shouldChangesBeDispalyed ? _prosemirrorChangeset.ChangeSet.create(tr.doc) : state.changes
40
- });
41
- }
37
+ if (metadata && metadata.action === _types.TOGGLE_TRACK_CHANGES_ACTION.TOGGLE_TRACK_CHANGES) {
38
+ resetBaseline = true;
39
+ return _objectSpread(_objectSpread({}, state), {}, {
40
+ shouldChangesBeDisplayed: !state.shouldChangesBeDisplayed
41
+ });
42
42
  }
43
- if (!tr.docChanged || tr.getMeta('isRemote')) {
43
+ var isDocChanged = tr.docChanged && tr.steps.some(function (step) {
44
+ return step instanceof _transform.ReplaceStep || step instanceof _transform.ReplaceAroundStep;
45
+ });
46
+ if (!isDocChanged || tr.getMeta('isRemote') || tr.getMeta('addToHistory') === false) {
44
47
  // If no document changes, return the old changeSet
45
48
  return state;
46
49
  }
50
+ var changes = resetBaseline ? _prosemirrorChangeset.ChangeSet.create(tr.docs[0]).addSteps(tr.doc,
51
+ // The new document
52
+ tr.mapping.maps,
53
+ // The set of changes
54
+ tr.docs[0].content // The old document
55
+ ) : state.changes.addSteps(tr.doc,
56
+ // The new document
57
+ tr.mapping.maps,
58
+ // The set of changes
59
+ tr.docs[0].content // The old document
60
+ );
61
+ var baselineDoc = resetBaseline ? tr.docs[0] : state.baselineDoc;
62
+ resetBaseline = false;
47
63
 
48
64
  // Create a new ChangeSet based on document changes
49
65
  return _objectSpread(_objectSpread({}, state), {}, {
50
- shouldChangesBeDispalyed: false,
51
- changes: state.changes.addSteps(tr.doc,
52
- // The new document
53
- tr.mapping.maps,
54
- // The set of changes
55
- tr.docs[0].content // The old document
56
- )
66
+ baselineDoc: baselineDoc,
67
+ shouldChangesBeDisplayed: false,
68
+ changes: changes,
69
+ isShowDiffAvailable: true
57
70
  });
58
71
  }
59
72
  },
60
73
  props: {
61
74
  decorations: function decorations(state) {
62
75
  var pluginState = trackChangesPluginKey.getState(state);
63
- if (pluginState && pluginState.shouldChangesBeDispalyed && pluginState.changes) {
64
- var decoration = _view.DecorationSet.empty;
65
- var decorations = [];
66
- var changes = (0, _prosemirrorChangeset.simplifyChanges)(pluginState.changes.changes, state.doc);
67
- var tr = state.tr;
68
- changes.forEach(function (change) {
69
- if (change.inserted.length > 0) {
70
- var style = (0, _lazyNodeView.convertToInlineCss)({
71
- background: "var(--ds-background-accent-purple-subtlest, #F3F0FF)",
72
- textDecoration: 'underline',
73
- textDecorationStyle: 'dotted',
74
- textDecorationThickness: "var(--ds-space-025, 2px)",
75
- textDecorationColor: "var(--ds-border-accent-purple, #8270DB)"
76
- });
77
-
78
- // Inline decoration used for insertions as the content already exists in the document
79
- // and we just want to style it.
80
- var insertionInlineDecoration = _view.Decoration.inline(change.fromB, change.toB, {
81
- style: style
82
- }, {});
83
- decorations.push(insertionInlineDecoration);
84
- }
85
- if (change.deleted.length > 0) {
86
- var dom = document.createElement('span');
87
- var _style = (0, _lazyNodeView.convertToInlineCss)({
88
- color: "var(--ds-text-accent-gray, #44546F)",
89
- textDecoration: 'line-through'
90
- });
91
- dom.setAttribute('style', _style);
92
- dom.appendChild(_model.DOMSerializer.fromSchema(tr.doc.type.schema).serializeFragment(pluginState === null || pluginState === void 0 ? void 0 : pluginState.baselineDoc.slice(change.fromA, change.toA).content));
93
-
94
- // Widget decoration used for deletions as the content is not in the document
95
- // and we want to display the deleted content with a style.
96
- var deletionWidgetDecoration = _view.Decoration.widget(change.fromB, dom, {
97
- marks: []
98
- });
99
- decorations.push(deletionWidgetDecoration);
100
- }
101
- });
102
- return decoration.add(tr.doc, decorations);
76
+ if (!(pluginState !== null && pluginState !== void 0 && pluginState.shouldChangesBeDisplayed) || !pluginState.changes) {
77
+ return undefined;
103
78
  }
104
- return undefined;
79
+ var decoration = _view.DecorationSet.empty;
80
+ var decorations = [];
81
+ var changes = (0, _prosemirrorChangeset.simplifyChanges)(pluginState.changes.changes, state.doc);
82
+ var tr = state.tr;
83
+ changes.forEach(function (change) {
84
+ if (change.inserted.length > 0) {
85
+ decorations.push((0, _decorations.createInlineChangedDecoration)(change));
86
+ }
87
+ if (change.deleted.length > 0) {
88
+ decorations.push((0, _decorations.createDeletedContentDecoration)({
89
+ change: change,
90
+ doc: pluginState === null || pluginState === void 0 ? void 0 : pluginState.baselineDoc,
91
+ tr: tr
92
+ }));
93
+ }
94
+ });
95
+ return decoration.add(tr.doc, decorations);
105
96
  }
106
97
  }
107
98
  });
@@ -24,14 +24,16 @@ var trackChangesPlugin = exports.trackChangesPlugin = function trackChangesPlugi
24
24
  }
25
25
  },
26
26
  getSharedState: function getSharedState(editorState) {
27
- var _trackChangesPluginKe;
27
+ var _trackChangesPluginKe, _trackChangesPluginKe2;
28
28
  if (!editorState) {
29
29
  return {
30
- isDisplayingChanges: false
30
+ isDisplayingChanges: false,
31
+ isShowDiffAvailable: false
31
32
  };
32
33
  }
33
34
  return {
34
- isDisplayingChanges: Boolean((_trackChangesPluginKe = _main.trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe === void 0 ? void 0 : _trackChangesPluginKe.enable)
35
+ isDisplayingChanges: Boolean((_trackChangesPluginKe = _main.trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe === void 0 ? void 0 : _trackChangesPluginKe.shouldChangesBeDisplayed),
36
+ isShowDiffAvailable: Boolean((_trackChangesPluginKe2 = _main.trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe2 === void 0 ? void 0 : _trackChangesPluginKe2.isShowDiffAvailable)
35
37
  };
36
38
  }
37
39
  };
@@ -0,0 +1,48 @@
1
+ import { convertToInlineCss } from '@atlaskit/editor-common/lazy-node-view';
2
+ import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
3
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
4
+ const style = convertToInlineCss({
5
+ background: "var(--ds-background-accent-purple-subtlest, #F3F0FF)",
6
+ textDecoration: 'underline',
7
+ textDecorationStyle: 'dotted',
8
+ textDecorationThickness: "var(--ds-space-025, 2px)",
9
+ textDecorationColor: "var(--ds-border-accent-purple, #8270DB)"
10
+ });
11
+
12
+ /**
13
+ * Inline decoration used for insertions as the content already exists in the document
14
+ *
15
+ * @param change Changeset "change" containing information about the change content + range
16
+ * @returns Prosemirror inline decoration
17
+ */
18
+ export const createInlineChangedDecoration = change => Decoration.inline(change.fromB, change.toB, {
19
+ style
20
+ }, {});
21
+ const deletedContentStyle = convertToInlineCss({
22
+ color: "var(--ds-text-accent-gray, #44546F)",
23
+ textDecoration: 'line-through'
24
+ });
25
+
26
+ /**
27
+ * Creates a widget to represent the deleted content in the editor
28
+ *
29
+ * @param props.change Changeset "change" containing information about the change content + range
30
+ * @param props.doc Baseline doc to compare against
31
+ * @param props.tr The relevant transaction this decoration is being created against
32
+ * @returns Prosemirror widget decoration
33
+ */
34
+ export const createDeletedContentDecoration = ({
35
+ change,
36
+ doc,
37
+ tr
38
+ }) => {
39
+ const dom = document.createElement('span');
40
+ dom.setAttribute('style', deletedContentStyle);
41
+ dom.appendChild(DOMSerializer.fromSchema(tr.doc.type.schema).serializeFragment(doc.slice(change.fromA, change.toA).content));
42
+
43
+ // Widget decoration used for deletions as the content is not in the document
44
+ // and we want to display the deleted content with a style.
45
+ return Decoration.widget(change.fromB, dom, {
46
+ marks: []
47
+ });
48
+ };
@@ -1,12 +1,14 @@
1
1
  import { ChangeSet, simplifyChanges } from 'prosemirror-changeset';
2
- import { convertToInlineCss } from '@atlaskit/editor-common/lazy-node-view';
3
2
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
4
- import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
5
3
  import { PluginKey } from '@atlaskit/editor-prosemirror/state';
6
- import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view';
4
+ import { ReplaceAroundStep, ReplaceStep } from '@atlaskit/editor-prosemirror/transform';
5
+ import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
6
+ import { createInlineChangedDecoration, createDeletedContentDecoration } from './decorations';
7
7
  import { TOGGLE_TRACK_CHANGES_ACTION as ACTION } from './types';
8
8
  export const trackChangesPluginKey = new PluginKey('trackChangesPlugin');
9
9
  export const createTrackChangesPlugin = () => {
10
+ // Mark the state to be reset on next time the document has a meaningful change
11
+ let resetBaseline = false;
10
12
  return new SafePlugin({
11
13
  key: trackChangesPluginKey,
12
14
  state: {
@@ -15,86 +17,73 @@ export const createTrackChangesPlugin = () => {
15
17
  }) {
16
18
  return {
17
19
  changes: ChangeSet.create(doc),
18
- shouldChangesBeDispalyed: false,
20
+ shouldChangesBeDisplayed: false,
21
+ isShowDiffAvailable: false,
19
22
  baselineDoc: doc,
20
23
  numOfChanges: 0
21
24
  };
22
25
  },
23
26
  apply(tr, state, oldState, newState) {
24
27
  const metadata = tr.getMeta(trackChangesPluginKey);
25
- if (metadata) {
26
- if (metadata.action === ACTION.TOGGLE_TRACK_CHANGES) {
27
- return {
28
- ...state,
29
- baselineDoc: state.shouldChangesBeDispalyed ? tr.doc : state.baselineDoc,
30
- shouldChangesBeDispalyed: !state.shouldChangesBeDispalyed,
31
- changes: state.shouldChangesBeDispalyed ? ChangeSet.create(tr.doc) : state.changes
32
- };
33
- }
28
+ if (metadata && metadata.action === ACTION.TOGGLE_TRACK_CHANGES) {
29
+ resetBaseline = true;
30
+ return {
31
+ ...state,
32
+ shouldChangesBeDisplayed: !state.shouldChangesBeDisplayed
33
+ };
34
34
  }
35
- if (!tr.docChanged || tr.getMeta('isRemote')) {
35
+ const isDocChanged = tr.docChanged && tr.steps.some(step => step instanceof ReplaceStep || step instanceof ReplaceAroundStep);
36
+ if (!isDocChanged || tr.getMeta('isRemote') || tr.getMeta('addToHistory') === false) {
36
37
  // If no document changes, return the old changeSet
37
38
  return state;
38
39
  }
40
+ const changes = resetBaseline ? ChangeSet.create(tr.docs[0]).addSteps(tr.doc,
41
+ // The new document
42
+ tr.mapping.maps,
43
+ // The set of changes
44
+ tr.docs[0].content // The old document
45
+ ) : state.changes.addSteps(tr.doc,
46
+ // The new document
47
+ tr.mapping.maps,
48
+ // The set of changes
49
+ tr.docs[0].content // The old document
50
+ );
51
+ const baselineDoc = resetBaseline ? tr.docs[0] : state.baselineDoc;
52
+ resetBaseline = false;
39
53
 
40
54
  // Create a new ChangeSet based on document changes
41
55
  return {
42
56
  ...state,
43
- shouldChangesBeDispalyed: false,
44
- changes: state.changes.addSteps(tr.doc,
45
- // The new document
46
- tr.mapping.maps,
47
- // The set of changes
48
- tr.docs[0].content // The old document
49
- )
57
+ baselineDoc,
58
+ shouldChangesBeDisplayed: false,
59
+ changes,
60
+ isShowDiffAvailable: true
50
61
  };
51
62
  }
52
63
  },
53
64
  props: {
54
65
  decorations: state => {
55
66
  const pluginState = trackChangesPluginKey.getState(state);
56
- if (pluginState && pluginState.shouldChangesBeDispalyed && pluginState.changes) {
57
- const decoration = DecorationSet.empty;
58
- const decorations = [];
59
- const changes = simplifyChanges(pluginState.changes.changes, state.doc);
60
- const tr = state.tr;
61
- changes.forEach(change => {
62
- if (change.inserted.length > 0) {
63
- const style = convertToInlineCss({
64
- background: "var(--ds-background-accent-purple-subtlest, #F3F0FF)",
65
- textDecoration: 'underline',
66
- textDecorationStyle: 'dotted',
67
- textDecorationThickness: "var(--ds-space-025, 2px)",
68
- textDecorationColor: "var(--ds-border-accent-purple, #8270DB)"
69
- });
70
-
71
- // Inline decoration used for insertions as the content already exists in the document
72
- // and we just want to style it.
73
- const insertionInlineDecoration = Decoration.inline(change.fromB, change.toB, {
74
- style
75
- }, {});
76
- decorations.push(insertionInlineDecoration);
77
- }
78
- if (change.deleted.length > 0) {
79
- const dom = document.createElement('span');
80
- const style = convertToInlineCss({
81
- color: "var(--ds-text-accent-gray, #44546F)",
82
- textDecoration: 'line-through'
83
- });
84
- dom.setAttribute('style', style);
85
- dom.appendChild(DOMSerializer.fromSchema(tr.doc.type.schema).serializeFragment(pluginState === null || pluginState === void 0 ? void 0 : pluginState.baselineDoc.slice(change.fromA, change.toA).content));
86
-
87
- // Widget decoration used for deletions as the content is not in the document
88
- // and we want to display the deleted content with a style.
89
- const deletionWidgetDecoration = Decoration.widget(change.fromB, dom, {
90
- marks: []
91
- });
92
- decorations.push(deletionWidgetDecoration);
93
- }
94
- });
95
- return decoration.add(tr.doc, decorations);
67
+ if (!(pluginState !== null && pluginState !== void 0 && pluginState.shouldChangesBeDisplayed) || !pluginState.changes) {
68
+ return undefined;
96
69
  }
97
- return undefined;
70
+ const decoration = DecorationSet.empty;
71
+ const decorations = [];
72
+ const changes = simplifyChanges(pluginState.changes.changes, state.doc);
73
+ const tr = state.tr;
74
+ changes.forEach(change => {
75
+ if (change.inserted.length > 0) {
76
+ decorations.push(createInlineChangedDecoration(change));
77
+ }
78
+ if (change.deleted.length > 0) {
79
+ decorations.push(createDeletedContentDecoration({
80
+ change,
81
+ doc: pluginState === null || pluginState === void 0 ? void 0 : pluginState.baselineDoc,
82
+ tr
83
+ }));
84
+ }
85
+ });
86
+ return decoration.add(tr.doc, decorations);
98
87
  }
99
88
  }
100
89
  });
@@ -18,14 +18,16 @@ export const trackChangesPlugin = () => ({
18
18
  }
19
19
  },
20
20
  getSharedState: editorState => {
21
- var _trackChangesPluginKe;
21
+ var _trackChangesPluginKe, _trackChangesPluginKe2;
22
22
  if (!editorState) {
23
23
  return {
24
- isDisplayingChanges: false
24
+ isDisplayingChanges: false,
25
+ isShowDiffAvailable: false
25
26
  };
26
27
  }
27
28
  return {
28
- isDisplayingChanges: Boolean((_trackChangesPluginKe = trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe === void 0 ? void 0 : _trackChangesPluginKe.enable)
29
+ isDisplayingChanges: Boolean((_trackChangesPluginKe = trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe === void 0 ? void 0 : _trackChangesPluginKe.shouldChangesBeDisplayed),
30
+ isShowDiffAvailable: Boolean((_trackChangesPluginKe2 = trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe2 === void 0 ? void 0 : _trackChangesPluginKe2.isShowDiffAvailable)
29
31
  };
30
32
  }
31
33
  });
@@ -0,0 +1,49 @@
1
+ import { convertToInlineCss } from '@atlaskit/editor-common/lazy-node-view';
2
+ import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
3
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
4
+ var style = convertToInlineCss({
5
+ background: "var(--ds-background-accent-purple-subtlest, #F3F0FF)",
6
+ textDecoration: 'underline',
7
+ textDecorationStyle: 'dotted',
8
+ textDecorationThickness: "var(--ds-space-025, 2px)",
9
+ textDecorationColor: "var(--ds-border-accent-purple, #8270DB)"
10
+ });
11
+
12
+ /**
13
+ * Inline decoration used for insertions as the content already exists in the document
14
+ *
15
+ * @param change Changeset "change" containing information about the change content + range
16
+ * @returns Prosemirror inline decoration
17
+ */
18
+ export var createInlineChangedDecoration = function createInlineChangedDecoration(change) {
19
+ return Decoration.inline(change.fromB, change.toB, {
20
+ style: style
21
+ }, {});
22
+ };
23
+ var deletedContentStyle = convertToInlineCss({
24
+ color: "var(--ds-text-accent-gray, #44546F)",
25
+ textDecoration: 'line-through'
26
+ });
27
+
28
+ /**
29
+ * Creates a widget to represent the deleted content in the editor
30
+ *
31
+ * @param props.change Changeset "change" containing information about the change content + range
32
+ * @param props.doc Baseline doc to compare against
33
+ * @param props.tr The relevant transaction this decoration is being created against
34
+ * @returns Prosemirror widget decoration
35
+ */
36
+ export var createDeletedContentDecoration = function createDeletedContentDecoration(_ref) {
37
+ var change = _ref.change,
38
+ doc = _ref.doc,
39
+ tr = _ref.tr;
40
+ var dom = document.createElement('span');
41
+ dom.setAttribute('style', deletedContentStyle);
42
+ dom.appendChild(DOMSerializer.fromSchema(tr.doc.type.schema).serializeFragment(doc.slice(change.fromA, change.toA).content));
43
+
44
+ // Widget decoration used for deletions as the content is not in the document
45
+ // and we want to display the deleted content with a style.
46
+ return Decoration.widget(change.fromB, dom, {
47
+ marks: []
48
+ });
49
+ };
@@ -2,14 +2,16 @@ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  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; }
3
3
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
4
  import { ChangeSet, simplifyChanges } from 'prosemirror-changeset';
5
- import { convertToInlineCss } from '@atlaskit/editor-common/lazy-node-view';
6
5
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
7
- import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
8
6
  import { PluginKey } from '@atlaskit/editor-prosemirror/state';
9
- import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view';
7
+ import { ReplaceAroundStep, ReplaceStep } from '@atlaskit/editor-prosemirror/transform';
8
+ import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
9
+ import { createInlineChangedDecoration, createDeletedContentDecoration } from './decorations';
10
10
  import { TOGGLE_TRACK_CHANGES_ACTION as ACTION } from './types';
11
11
  export var trackChangesPluginKey = new PluginKey('trackChangesPlugin');
12
12
  export var createTrackChangesPlugin = function createTrackChangesPlugin() {
13
+ // Mark the state to be reset on next time the document has a meaningful change
14
+ var resetBaseline = false;
13
15
  return new SafePlugin({
14
16
  key: trackChangesPluginKey,
15
17
  state: {
@@ -17,84 +19,73 @@ export var createTrackChangesPlugin = function createTrackChangesPlugin() {
17
19
  var doc = _ref.doc;
18
20
  return {
19
21
  changes: ChangeSet.create(doc),
20
- shouldChangesBeDispalyed: false,
22
+ shouldChangesBeDisplayed: false,
23
+ isShowDiffAvailable: false,
21
24
  baselineDoc: doc,
22
25
  numOfChanges: 0
23
26
  };
24
27
  },
25
28
  apply: function apply(tr, state, oldState, newState) {
26
29
  var metadata = tr.getMeta(trackChangesPluginKey);
27
- if (metadata) {
28
- if (metadata.action === ACTION.TOGGLE_TRACK_CHANGES) {
29
- return _objectSpread(_objectSpread({}, state), {}, {
30
- baselineDoc: state.shouldChangesBeDispalyed ? tr.doc : state.baselineDoc,
31
- shouldChangesBeDispalyed: !state.shouldChangesBeDispalyed,
32
- changes: state.shouldChangesBeDispalyed ? ChangeSet.create(tr.doc) : state.changes
33
- });
34
- }
30
+ if (metadata && metadata.action === ACTION.TOGGLE_TRACK_CHANGES) {
31
+ resetBaseline = true;
32
+ return _objectSpread(_objectSpread({}, state), {}, {
33
+ shouldChangesBeDisplayed: !state.shouldChangesBeDisplayed
34
+ });
35
35
  }
36
- if (!tr.docChanged || tr.getMeta('isRemote')) {
36
+ var isDocChanged = tr.docChanged && tr.steps.some(function (step) {
37
+ return step instanceof ReplaceStep || step instanceof ReplaceAroundStep;
38
+ });
39
+ if (!isDocChanged || tr.getMeta('isRemote') || tr.getMeta('addToHistory') === false) {
37
40
  // If no document changes, return the old changeSet
38
41
  return state;
39
42
  }
43
+ var changes = resetBaseline ? ChangeSet.create(tr.docs[0]).addSteps(tr.doc,
44
+ // The new document
45
+ tr.mapping.maps,
46
+ // The set of changes
47
+ tr.docs[0].content // The old document
48
+ ) : state.changes.addSteps(tr.doc,
49
+ // The new document
50
+ tr.mapping.maps,
51
+ // The set of changes
52
+ tr.docs[0].content // The old document
53
+ );
54
+ var baselineDoc = resetBaseline ? tr.docs[0] : state.baselineDoc;
55
+ resetBaseline = false;
40
56
 
41
57
  // Create a new ChangeSet based on document changes
42
58
  return _objectSpread(_objectSpread({}, state), {}, {
43
- shouldChangesBeDispalyed: false,
44
- changes: state.changes.addSteps(tr.doc,
45
- // The new document
46
- tr.mapping.maps,
47
- // The set of changes
48
- tr.docs[0].content // The old document
49
- )
59
+ baselineDoc: baselineDoc,
60
+ shouldChangesBeDisplayed: false,
61
+ changes: changes,
62
+ isShowDiffAvailable: true
50
63
  });
51
64
  }
52
65
  },
53
66
  props: {
54
67
  decorations: function decorations(state) {
55
68
  var pluginState = trackChangesPluginKey.getState(state);
56
- if (pluginState && pluginState.shouldChangesBeDispalyed && pluginState.changes) {
57
- var decoration = DecorationSet.empty;
58
- var decorations = [];
59
- var changes = simplifyChanges(pluginState.changes.changes, state.doc);
60
- var tr = state.tr;
61
- changes.forEach(function (change) {
62
- if (change.inserted.length > 0) {
63
- var style = convertToInlineCss({
64
- background: "var(--ds-background-accent-purple-subtlest, #F3F0FF)",
65
- textDecoration: 'underline',
66
- textDecorationStyle: 'dotted',
67
- textDecorationThickness: "var(--ds-space-025, 2px)",
68
- textDecorationColor: "var(--ds-border-accent-purple, #8270DB)"
69
- });
70
-
71
- // Inline decoration used for insertions as the content already exists in the document
72
- // and we just want to style it.
73
- var insertionInlineDecoration = Decoration.inline(change.fromB, change.toB, {
74
- style: style
75
- }, {});
76
- decorations.push(insertionInlineDecoration);
77
- }
78
- if (change.deleted.length > 0) {
79
- var dom = document.createElement('span');
80
- var _style = convertToInlineCss({
81
- color: "var(--ds-text-accent-gray, #44546F)",
82
- textDecoration: 'line-through'
83
- });
84
- dom.setAttribute('style', _style);
85
- dom.appendChild(DOMSerializer.fromSchema(tr.doc.type.schema).serializeFragment(pluginState === null || pluginState === void 0 ? void 0 : pluginState.baselineDoc.slice(change.fromA, change.toA).content));
86
-
87
- // Widget decoration used for deletions as the content is not in the document
88
- // and we want to display the deleted content with a style.
89
- var deletionWidgetDecoration = Decoration.widget(change.fromB, dom, {
90
- marks: []
91
- });
92
- decorations.push(deletionWidgetDecoration);
93
- }
94
- });
95
- return decoration.add(tr.doc, decorations);
69
+ if (!(pluginState !== null && pluginState !== void 0 && pluginState.shouldChangesBeDisplayed) || !pluginState.changes) {
70
+ return undefined;
96
71
  }
97
- return undefined;
72
+ var decoration = DecorationSet.empty;
73
+ var decorations = [];
74
+ var changes = simplifyChanges(pluginState.changes.changes, state.doc);
75
+ var tr = state.tr;
76
+ changes.forEach(function (change) {
77
+ if (change.inserted.length > 0) {
78
+ decorations.push(createInlineChangedDecoration(change));
79
+ }
80
+ if (change.deleted.length > 0) {
81
+ decorations.push(createDeletedContentDecoration({
82
+ change: change,
83
+ doc: pluginState === null || pluginState === void 0 ? void 0 : pluginState.baselineDoc,
84
+ tr: tr
85
+ }));
86
+ }
87
+ });
88
+ return decoration.add(tr.doc, decorations);
98
89
  }
99
90
  }
100
91
  });
@@ -18,14 +18,16 @@ export var trackChangesPlugin = function trackChangesPlugin() {
18
18
  }
19
19
  },
20
20
  getSharedState: function getSharedState(editorState) {
21
- var _trackChangesPluginKe;
21
+ var _trackChangesPluginKe, _trackChangesPluginKe2;
22
22
  if (!editorState) {
23
23
  return {
24
- isDisplayingChanges: false
24
+ isDisplayingChanges: false,
25
+ isShowDiffAvailable: false
25
26
  };
26
27
  }
27
28
  return {
28
- isDisplayingChanges: Boolean((_trackChangesPluginKe = trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe === void 0 ? void 0 : _trackChangesPluginKe.enable)
29
+ isDisplayingChanges: Boolean((_trackChangesPluginKe = trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe === void 0 ? void 0 : _trackChangesPluginKe.shouldChangesBeDisplayed),
30
+ isShowDiffAvailable: Boolean((_trackChangesPluginKe2 = trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe2 === void 0 ? void 0 : _trackChangesPluginKe2.isShowDiffAvailable)
29
31
  };
30
32
  }
31
33
  };
@@ -0,0 +1,26 @@
1
+ import type { Change } from 'prosemirror-changeset';
2
+ import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
3
+ import type { Transaction } from '@atlaskit/editor-prosemirror/state';
4
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
5
+ /**
6
+ * Inline decoration used for insertions as the content already exists in the document
7
+ *
8
+ * @param change Changeset "change" containing information about the change content + range
9
+ * @returns Prosemirror inline decoration
10
+ */
11
+ export declare const createInlineChangedDecoration: (change: Change) => Decoration;
12
+ interface DeletedContentDecorationProps {
13
+ change: Change;
14
+ doc: PMNode;
15
+ tr: Transaction;
16
+ }
17
+ /**
18
+ * Creates a widget to represent the deleted content in the editor
19
+ *
20
+ * @param props.change Changeset "change" containing information about the change content + range
21
+ * @param props.doc Baseline doc to compare against
22
+ * @param props.tr The relevant transaction this decoration is being created against
23
+ * @returns Prosemirror widget decoration
24
+ */
25
+ export declare const createDeletedContentDecoration: ({ change, doc, tr, }: DeletedContentDecorationProps) => Decoration;
26
+ export {};
@@ -2,9 +2,10 @@ import { ChangeSet } from 'prosemirror-changeset';
2
2
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
3
3
  import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
4
4
  import { PluginKey } from '@atlaskit/editor-prosemirror/state';
5
- export declare const trackChangesPluginKey: PluginKey<any>;
5
+ export declare const trackChangesPluginKey: PluginKey<TrackChangesPluginState>;
6
6
  type TrackChangesPluginState = {
7
- shouldChangesBeDispalyed: boolean;
7
+ shouldChangesBeDisplayed: boolean;
8
+ isShowDiffAvailable: boolean;
8
9
  baselineDoc: PMNode;
9
10
  changes: ChangeSet;
10
11
  };
@@ -12,5 +12,11 @@ export type TrackChangesPlugin = NextEditorPlugin<'trackChanges', {
12
12
  * Defaults to false.
13
13
  */
14
14
  isDisplayingChanges: boolean;
15
+ /**
16
+ * If there are changes in the document that determine if track changes button
17
+ * should be enabled.
18
+ * This will only be false initially before any changes in the session.
19
+ */
20
+ isShowDiffAvailable: boolean;
15
21
  };
16
22
  }>;
@@ -0,0 +1,26 @@
1
+ import type { Change } from 'prosemirror-changeset';
2
+ import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
3
+ import type { Transaction } from '@atlaskit/editor-prosemirror/state';
4
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
5
+ /**
6
+ * Inline decoration used for insertions as the content already exists in the document
7
+ *
8
+ * @param change Changeset "change" containing information about the change content + range
9
+ * @returns Prosemirror inline decoration
10
+ */
11
+ export declare const createInlineChangedDecoration: (change: Change) => Decoration;
12
+ interface DeletedContentDecorationProps {
13
+ change: Change;
14
+ doc: PMNode;
15
+ tr: Transaction;
16
+ }
17
+ /**
18
+ * Creates a widget to represent the deleted content in the editor
19
+ *
20
+ * @param props.change Changeset "change" containing information about the change content + range
21
+ * @param props.doc Baseline doc to compare against
22
+ * @param props.tr The relevant transaction this decoration is being created against
23
+ * @returns Prosemirror widget decoration
24
+ */
25
+ export declare const createDeletedContentDecoration: ({ change, doc, tr, }: DeletedContentDecorationProps) => Decoration;
26
+ export {};
@@ -2,9 +2,10 @@ import { ChangeSet } from 'prosemirror-changeset';
2
2
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
3
3
  import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
4
4
  import { PluginKey } from '@atlaskit/editor-prosemirror/state';
5
- export declare const trackChangesPluginKey: PluginKey<any>;
5
+ export declare const trackChangesPluginKey: PluginKey<TrackChangesPluginState>;
6
6
  type TrackChangesPluginState = {
7
- shouldChangesBeDispalyed: boolean;
7
+ shouldChangesBeDisplayed: boolean;
8
+ isShowDiffAvailable: boolean;
8
9
  baselineDoc: PMNode;
9
10
  changes: ChangeSet;
10
11
  };
@@ -12,5 +12,11 @@ export type TrackChangesPlugin = NextEditorPlugin<'trackChanges', {
12
12
  * Defaults to false.
13
13
  */
14
14
  isDisplayingChanges: boolean;
15
+ /**
16
+ * If there are changes in the document that determine if track changes button
17
+ * should be enabled.
18
+ * This will only be false initially before any changes in the session.
19
+ */
20
+ isShowDiffAvailable: boolean;
15
21
  };
16
22
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-track-changes",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "ShowDiff plugin for @atlaskit/editor-core",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -32,12 +32,12 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@atlaskit/editor-prosemirror": "7.0.0",
35
- "@atlaskit/tokens": "^5.5.0",
35
+ "@atlaskit/tokens": "^5.6.0",
36
36
  "@babel/runtime": "^7.0.0",
37
37
  "prosemirror-changeset": "^2.2.1"
38
38
  },
39
39
  "peerDependencies": {
40
- "@atlaskit/editor-common": "^107.7.0",
40
+ "@atlaskit/editor-common": "^107.12.0",
41
41
  "react": "^18.2.0"
42
42
  },
43
43
  "devDependencies": {