@atlaskit/editor-plugin-track-changes 2.1.0 → 2.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,25 @@
1
1
  # @atlaskit/editor-plugin-track-changes
2
2
 
3
+ ## 2.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#193685](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/193685)
8
+ [`ee3ba46cb3d0a`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/ee3ba46cb3d0a) -
9
+ [EDITOR-1073] Add undo/ redo & show diff to comment toolbar
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies
14
+
15
+ ## 2.1.1
16
+
17
+ ### Patch Changes
18
+
19
+ - [#190819](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/190819)
20
+ [`20420774e83cc`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/20420774e83cc) -
21
+ Include Track chages plugin
22
+
3
23
  ## 2.1.0
4
24
 
5
25
  ### Minor Changes
@@ -18,7 +18,16 @@
18
18
  ],
19
19
  "references": [
20
20
  {
21
- "path": "../../../design-system/tokens/afm-cc/tsconfig.json"
21
+ "path": "../../../design-system/button/afm-cc/tsconfig.json"
22
+ },
23
+ {
24
+ "path": "../../editor-plugin-primary-toolbar/afm-cc/tsconfig.json"
25
+ },
26
+ {
27
+ "path": "../../editor-plugin-show-diff/afm-cc/tsconfig.json"
28
+ },
29
+ {
30
+ "path": "../../../design-system/icon-lab/afm-cc/tsconfig.json"
22
31
  },
23
32
  {
24
33
  "path": "../../editor-common/afm-cc/tsconfig.json"
@@ -2,7 +2,7 @@
2
2
  "extends": "../../../../tsconfig.entry-points.jira.json",
3
3
  "compilerOptions": {
4
4
  "target": "es5",
5
- "outDir": "../../../../../tsDist/@atlaskit__editor-plugin-toolbar-lists-indentation/app",
5
+ "outDir": "../../../../../tsDist/@atlaskit__editor-plugin-track-changes/app",
6
6
  "rootDir": "../",
7
7
  "composite": true
8
8
  },
@@ -18,46 +18,16 @@
18
18
  ],
19
19
  "references": [
20
20
  {
21
- "path": "../../../design-system/css/afm-jira/tsconfig.json"
22
- },
23
- {
24
- "path": "../../editor-plugin-analytics/afm-jira/tsconfig.json"
25
- },
26
- {
27
- "path": "../../editor-plugin-block-type/afm-jira/tsconfig.json"
28
- },
29
- {
30
- "path": "../../editor-plugin-feature-flags/afm-jira/tsconfig.json"
31
- },
32
- {
33
- "path": "../../editor-plugin-indentation/afm-jira/tsconfig.json"
34
- },
35
- {
36
- "path": "../../editor-plugin-list/afm-jira/tsconfig.json"
21
+ "path": "../../../design-system/button/afm-jira/tsconfig.json"
37
22
  },
38
23
  {
39
24
  "path": "../../editor-plugin-primary-toolbar/afm-jira/tsconfig.json"
40
25
  },
41
26
  {
42
- "path": "../../editor-plugin-selection-toolbar/afm-jira/tsconfig.json"
43
- },
44
- {
45
- "path": "../../editor-plugin-tasks-and-decisions/afm-jira/tsconfig.json"
46
- },
47
- {
48
- "path": "../../editor-shared-styles/afm-jira/tsconfig.json"
49
- },
50
- {
51
- "path": "../../../design-system/icon/afm-jira/tsconfig.json"
52
- },
53
- {
54
- "path": "../../../platform/feature-flags/afm-jira/tsconfig.json"
55
- },
56
- {
57
- "path": "../../tmp-editor-statsig/afm-jira/tsconfig.json"
27
+ "path": "../../editor-plugin-show-diff/afm-jira/tsconfig.json"
58
28
  },
59
29
  {
60
- "path": "../../../design-system/tokens/afm-jira/tsconfig.json"
30
+ "path": "../../../design-system/icon-lab/afm-jira/tsconfig.json"
61
31
  },
62
32
  {
63
33
  "path": "../../editor-common/afm-jira/tsconfig.json"
@@ -2,7 +2,7 @@
2
2
  "extends": "../../../../tsconfig.entry-points.post-office.json",
3
3
  "compilerOptions": {
4
4
  "target": "es5",
5
- "outDir": "../../../../../post-office/tsDist/@atlaskit__editor-plugin-toolbar-lists-indentation/app",
5
+ "outDir": "../../../../../post-office/tsDist/@atlaskit__editor-plugin-track-changes/app",
6
6
  "rootDir": "../",
7
7
  "composite": true
8
8
  },
@@ -18,10 +18,19 @@
18
18
  ],
19
19
  "references": [
20
20
  {
21
- "path": "../editor-common/tsconfig.app.json"
21
+ "path": "../../../design-system/button/afm-post-office/tsconfig.json"
22
22
  },
23
23
  {
24
- "path": "../../design-system/tokens/tsconfig.app.json"
24
+ "path": "../../editor-plugin-primary-toolbar/afm-post-office/tsconfig.json"
25
+ },
26
+ {
27
+ "path": "../../editor-plugin-show-diff/afm-post-office/tsconfig.json"
28
+ },
29
+ {
30
+ "path": "../../../design-system/icon-lab/afm-post-office/tsconfig.json"
31
+ },
32
+ {
33
+ "path": "../../editor-common/afm-post-office/tsconfig.json"
25
34
  }
26
35
  ]
27
36
  }
@@ -0,0 +1,36 @@
1
+ {
2
+ "extends": "../../../../tsconfig.entry-points.rovo-extension.json",
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "outDir": "../../../../../rovo-extension/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/button/afm-rovo-extension/tsconfig.json"
22
+ },
23
+ {
24
+ "path": "../../editor-plugin-primary-toolbar/afm-rovo-extension/tsconfig.json"
25
+ },
26
+ {
27
+ "path": "../../editor-plugin-show-diff/afm-rovo-extension/tsconfig.json"
28
+ },
29
+ {
30
+ "path": "../../../design-system/icon-lab/afm-rovo-extension/tsconfig.json"
31
+ },
32
+ {
33
+ "path": "../../editor-common/afm-rovo-extension/tsconfig.json"
34
+ }
35
+ ]
36
+ }
@@ -0,0 +1,36 @@
1
+ {
2
+ "extends": "../../../../tsconfig.entry-points.townsquare.json",
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "outDir": "../../../../../townsquare/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/button/afm-townsquare/tsconfig.json"
22
+ },
23
+ {
24
+ "path": "../../editor-plugin-primary-toolbar/afm-townsquare/tsconfig.json"
25
+ },
26
+ {
27
+ "path": "../../editor-plugin-show-diff/afm-townsquare/tsconfig.json"
28
+ },
29
+ {
30
+ "path": "../../../design-system/icon-lab/afm-townsquare/tsconfig.json"
31
+ },
32
+ {
33
+ "path": "../../editor-common/afm-townsquare/tsconfig.json"
34
+ }
35
+ ]
36
+ }
@@ -4,35 +4,58 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.trackChangesPluginKey = exports.createTrackChangesPlugin = void 0;
7
+ exports.trackChangesPluginKey = exports.createTrackChangesPlugin = exports.InvertableStep = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
8
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
- var _prosemirrorChangeset = require("prosemirror-changeset");
10
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
11
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
10
12
  var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
11
13
  var _state = require("@atlaskit/editor-prosemirror/state");
12
14
  var _transform = require("@atlaskit/editor-prosemirror/transform");
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
+ 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; } } }; }
19
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
20
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
18
21
  var trackChangesPluginKey = exports.trackChangesPluginKey = new _state.PluginKey('trackChangesPlugin');
19
- var createTrackChangesPlugin = exports.createTrackChangesPlugin = function createTrackChangesPlugin() {
22
+ var InvertableStep = exports.InvertableStep = /*#__PURE__*/(0, _createClass2.default)(function InvertableStep(step, inverted) {
23
+ (0, _classCallCheck2.default)(this, InvertableStep);
24
+ this.step = step;
25
+ this.inverted = inverted;
26
+ });
27
+ var getBaselineFromSteps = function getBaselineFromSteps(doc, steps) {
28
+ var _iterator = _createForOfIteratorHelper(steps.slice().reverse()),
29
+ _step;
30
+ try {
31
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
32
+ var _step2 = _step.value;
33
+ var result = _step2.inverted.apply(doc);
34
+ if (result.failed === null && result.doc) {
35
+ doc = result.doc;
36
+ }
37
+ }
38
+ } catch (err) {
39
+ _iterator.e(err);
40
+ } finally {
41
+ _iterator.f();
42
+ }
43
+ return doc;
44
+ };
45
+ var createTrackChangesPlugin = exports.createTrackChangesPlugin = function createTrackChangesPlugin(api) {
20
46
  // Mark the state to be reset on next time the document has a meaningful change
21
47
  var resetBaseline = false;
22
48
  return new _safePlugin.SafePlugin({
23
49
  key: trackChangesPluginKey,
24
50
  state: {
25
- init: function init(_, _ref) {
26
- var doc = _ref.doc;
51
+ init: function init() {
27
52
  return {
28
- changes: _prosemirrorChangeset.ChangeSet.create(doc),
53
+ steps: [],
29
54
  shouldChangesBeDisplayed: false,
30
- isShowDiffAvailable: false,
31
- baselineDoc: doc,
32
- numOfChanges: 0
55
+ isShowDiffAvailable: false
33
56
  };
34
57
  },
35
- apply: function apply(tr, state, oldState, newState) {
58
+ apply: function apply(tr, state) {
36
59
  var metadata = tr.getMeta(trackChangesPluginKey);
37
60
  if (metadata && metadata.action === _types.TOGGLE_TRACK_CHANGES_ACTION.TOGGLE_TRACK_CHANGES) {
38
61
  resetBaseline = true;
@@ -47,53 +70,42 @@ var createTrackChangesPlugin = exports.createTrackChangesPlugin = function creat
47
70
  // If no document changes, return the old changeSet
48
71
  return state;
49
72
  }
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;
73
+ var newSteps = tr.steps.map(function (step, idx) {
74
+ return new InvertableStep(step, step.invert(tr.docs[idx]));
75
+ });
76
+ var steps = resetBaseline ? newSteps : [].concat((0, _toConsumableArray2.default)(state.steps), (0, _toConsumableArray2.default)(newSteps));
62
77
  resetBaseline = false;
63
78
 
64
79
  // Create a new ChangeSet based on document changes
65
80
  return _objectSpread(_objectSpread({}, state), {}, {
66
- baselineDoc: baselineDoc,
81
+ steps: steps,
67
82
  shouldChangesBeDisplayed: false,
68
- changes: changes,
69
83
  isShowDiffAvailable: true
70
84
  });
71
85
  }
72
86
  },
73
- props: {
74
- decorations: function decorations(state) {
75
- var pluginState = trackChangesPluginKey.getState(state);
76
- if (!(pluginState !== null && pluginState !== void 0 && pluginState.shouldChangesBeDisplayed) || !pluginState.changes) {
77
- return undefined;
78
- }
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
87
+ view: function view() {
88
+ return {
89
+ update: function update(view, prevState) {
90
+ var _trackChangesPluginKe;
91
+ var prevShouldChangesBeDisplayed = (_trackChangesPluginKe = trackChangesPluginKey.getState(prevState)) === null || _trackChangesPluginKe === void 0 ? void 0 : _trackChangesPluginKe.shouldChangesBeDisplayed;
92
+ var currentPluginState = trackChangesPluginKey.getState(view.state);
93
+ var currentShouldChangesBeDisplayed = currentPluginState === null || currentPluginState === void 0 ? void 0 : currentPluginState.shouldChangesBeDisplayed;
94
+ if (prevShouldChangesBeDisplayed === false && currentShouldChangesBeDisplayed === true) {
95
+ var _currentPluginState$s, _api$core, _api$showDiff;
96
+ var steps = (_currentPluginState$s = currentPluginState === null || currentPluginState === void 0 ? void 0 : currentPluginState.steps) !== null && _currentPluginState$s !== void 0 ? _currentPluginState$s : [];
97
+ api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 || _api$core.actions.execute(api === null || api === void 0 || (_api$showDiff = api.showDiff) === null || _api$showDiff === void 0 || (_api$showDiff = _api$showDiff.commands) === null || _api$showDiff === void 0 ? void 0 : _api$showDiff.showDiff({
98
+ originalDoc: getBaselineFromSteps(view.state.doc, steps),
99
+ steps: steps.map(function (s) {
100
+ return s.step;
101
+ })
92
102
  }));
103
+ } else if (currentShouldChangesBeDisplayed === false && prevShouldChangesBeDisplayed === true) {
104
+ var _api$core2, _api$showDiff2;
105
+ api === null || api === void 0 || (_api$core2 = api.core) === null || _api$core2 === void 0 || _api$core2.actions.execute(api === null || api === void 0 || (_api$showDiff2 = api.showDiff) === null || _api$showDiff2 === void 0 || (_api$showDiff2 = _api$showDiff2.commands) === null || _api$showDiff2 === void 0 ? void 0 : _api$showDiff2.hideDiff);
93
106
  }
94
- });
95
- return decoration.add(tr.doc, decorations);
96
- }
107
+ }
108
+ };
97
109
  }
98
110
  });
99
111
  };
@@ -1,23 +1,39 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
6
7
  exports.trackChangesPlugin = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
7
9
  var _main = require("./pm-plugins/main");
8
10
  var _types = require("./pm-plugins/types");
9
- var trackChangesPlugin = exports.trackChangesPlugin = function trackChangesPlugin() {
11
+ var _TrackChangesToolbarButton = require("./ui/TrackChangesToolbarButton");
12
+ var trackChangesPlugin = exports.trackChangesPlugin = function trackChangesPlugin(_ref) {
13
+ var _api$primaryToolbar;
14
+ var api = _ref.api;
15
+ var primaryToolbarComponent = function primaryToolbarComponent() {
16
+ return /*#__PURE__*/_react.default.createElement(_TrackChangesToolbarButton.TrackChangesToolbarButton, {
17
+ api: api
18
+ });
19
+ };
20
+ api === null || api === void 0 || (_api$primaryToolbar = api.primaryToolbar) === null || _api$primaryToolbar === void 0 || (_api$primaryToolbar = _api$primaryToolbar.actions) === null || _api$primaryToolbar === void 0 || _api$primaryToolbar.registerComponent({
21
+ name: 'trackChanges',
22
+ component: primaryToolbarComponent
23
+ });
10
24
  return {
11
25
  name: 'trackChanges',
12
26
  pmPlugins: function pmPlugins() {
13
27
  return [{
14
28
  name: 'trackChangesPlugin',
15
- plugin: _main.createTrackChangesPlugin
29
+ plugin: function plugin() {
30
+ return (0, _main.createTrackChangesPlugin)(api);
31
+ }
16
32
  }];
17
33
  },
18
34
  commands: {
19
- toggleChanges: function toggleChanges(_ref) {
20
- var tr = _ref.tr;
35
+ toggleChanges: function toggleChanges(_ref2) {
36
+ var tr = _ref2.tr;
21
37
  return tr.setMeta(_main.trackChangesPluginKey, {
22
38
  action: _types.TOGGLE_TRACK_CHANGES_ACTION.TOGGLE_TRACK_CHANGES
23
39
  });
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.TrackChangesToolbarButton = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var _new = require("@atlaskit/button/new");
10
+ var _hooks = require("@atlaskit/editor-common/hooks");
11
+ var _history = _interopRequireDefault(require("@atlaskit/icon-lab/core/history"));
12
+ var TrackChangesToolbarButton = exports.TrackChangesToolbarButton = function TrackChangesToolbarButton(_ref) {
13
+ var _api$trackChanges;
14
+ var api = _ref.api;
15
+ var _useSharedPluginState = (0, _hooks.useSharedPluginStateWithSelector)(api, ['trackChanges'], function (states) {
16
+ var _states$trackChangesS, _states$trackChangesS2;
17
+ return {
18
+ isDisplayingChanges: (_states$trackChangesS = states.trackChangesState) === null || _states$trackChangesS === void 0 ? void 0 : _states$trackChangesS.isDisplayingChanges,
19
+ isShowDiffAvailable: (_states$trackChangesS2 = states.trackChangesState) === null || _states$trackChangesS2 === void 0 ? void 0 : _states$trackChangesS2.isShowDiffAvailable
20
+ };
21
+ }),
22
+ isDisplayingChanges = _useSharedPluginState.isDisplayingChanges,
23
+ isShowDiffAvailable = _useSharedPluginState.isShowDiffAvailable;
24
+ var handleClick = _react.default.useCallback(function () {
25
+ api === null || api === void 0 || api.core.actions.execute(api === null || api === void 0 ? void 0 : api.trackChanges.commands.toggleChanges);
26
+ }, [api === null || api === void 0 || (_api$trackChanges = api.trackChanges) === null || _api$trackChanges === void 0 ? void 0 : _api$trackChanges.commands, api === null || api === void 0 ? void 0 : api.core.actions]);
27
+ return /*#__PURE__*/_react.default.createElement(_new.IconButton, {
28
+ icon: _history.default,
29
+ label: "Track Changes",
30
+ appearance: "subtle",
31
+ isDisabled: !isShowDiffAvailable,
32
+ isSelected: isDisplayingChanges,
33
+ onClick: handleClick
34
+ });
35
+ };
@@ -1,29 +1,37 @@
1
- import { ChangeSet, simplifyChanges } from 'prosemirror-changeset';
2
1
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
3
2
  import { PluginKey } from '@atlaskit/editor-prosemirror/state';
4
3
  import { ReplaceAroundStep, ReplaceStep } from '@atlaskit/editor-prosemirror/transform';
5
- import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
6
- import { createInlineChangedDecoration, createDeletedContentDecoration } from './decorations';
7
4
  import { TOGGLE_TRACK_CHANGES_ACTION as ACTION } from './types';
8
5
  export const trackChangesPluginKey = new PluginKey('trackChangesPlugin');
9
- export const createTrackChangesPlugin = () => {
6
+ export class InvertableStep {
7
+ constructor(step, inverted) {
8
+ this.step = step;
9
+ this.inverted = inverted;
10
+ }
11
+ }
12
+ const getBaselineFromSteps = (doc, steps) => {
13
+ for (const step of steps.slice().reverse()) {
14
+ const result = step.inverted.apply(doc);
15
+ if (result.failed === null && result.doc) {
16
+ doc = result.doc;
17
+ }
18
+ }
19
+ return doc;
20
+ };
21
+ export const createTrackChangesPlugin = api => {
10
22
  // Mark the state to be reset on next time the document has a meaningful change
11
23
  let resetBaseline = false;
12
24
  return new SafePlugin({
13
25
  key: trackChangesPluginKey,
14
26
  state: {
15
- init(_, {
16
- doc
17
- }) {
27
+ init() {
18
28
  return {
19
- changes: ChangeSet.create(doc),
29
+ steps: [],
20
30
  shouldChangesBeDisplayed: false,
21
- isShowDiffAvailable: false,
22
- baselineDoc: doc,
23
- numOfChanges: 0
31
+ isShowDiffAvailable: false
24
32
  };
25
33
  },
26
- apply(tr, state, oldState, newState) {
34
+ apply(tr, state) {
27
35
  const metadata = tr.getMeta(trackChangesPluginKey);
28
36
  if (metadata && metadata.action === ACTION.TOGGLE_TRACK_CHANGES) {
29
37
  resetBaseline = true;
@@ -37,54 +45,39 @@ export const createTrackChangesPlugin = () => {
37
45
  // If no document changes, return the old changeSet
38
46
  return state;
39
47
  }
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;
48
+ const newSteps = tr.steps.map((step, idx) => new InvertableStep(step, step.invert(tr.docs[idx])));
49
+ const steps = resetBaseline ? newSteps : [...state.steps, ...newSteps];
52
50
  resetBaseline = false;
53
51
 
54
52
  // Create a new ChangeSet based on document changes
55
53
  return {
56
54
  ...state,
57
- baselineDoc,
55
+ steps,
58
56
  shouldChangesBeDisplayed: false,
59
- changes,
60
57
  isShowDiffAvailable: true
61
58
  };
62
59
  }
63
60
  },
64
- props: {
65
- decorations: state => {
66
- const pluginState = trackChangesPluginKey.getState(state);
67
- if (!(pluginState !== null && pluginState !== void 0 && pluginState.shouldChangesBeDisplayed) || !pluginState.changes) {
68
- return undefined;
69
- }
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
61
+ view() {
62
+ return {
63
+ update(view, prevState) {
64
+ var _trackChangesPluginKe;
65
+ const prevShouldChangesBeDisplayed = (_trackChangesPluginKe = trackChangesPluginKey.getState(prevState)) === null || _trackChangesPluginKe === void 0 ? void 0 : _trackChangesPluginKe.shouldChangesBeDisplayed;
66
+ const currentPluginState = trackChangesPluginKey.getState(view.state);
67
+ const currentShouldChangesBeDisplayed = currentPluginState === null || currentPluginState === void 0 ? void 0 : currentPluginState.shouldChangesBeDisplayed;
68
+ if (prevShouldChangesBeDisplayed === false && currentShouldChangesBeDisplayed === true) {
69
+ var _currentPluginState$s, _api$core, _api$showDiff, _api$showDiff$command;
70
+ const steps = (_currentPluginState$s = currentPluginState === null || currentPluginState === void 0 ? void 0 : currentPluginState.steps) !== null && _currentPluginState$s !== void 0 ? _currentPluginState$s : [];
71
+ api === null || api === void 0 ? void 0 : (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.execute(api === null || api === void 0 ? void 0 : (_api$showDiff = api.showDiff) === null || _api$showDiff === void 0 ? void 0 : (_api$showDiff$command = _api$showDiff.commands) === null || _api$showDiff$command === void 0 ? void 0 : _api$showDiff$command.showDiff({
72
+ originalDoc: getBaselineFromSteps(view.state.doc, steps),
73
+ steps: steps.map(s => s.step)
83
74
  }));
75
+ } else if (currentShouldChangesBeDisplayed === false && prevShouldChangesBeDisplayed === true) {
76
+ var _api$core2, _api$showDiff2, _api$showDiff2$comman;
77
+ api === null || api === void 0 ? void 0 : (_api$core2 = api.core) === null || _api$core2 === void 0 ? void 0 : _api$core2.actions.execute(api === null || api === void 0 ? void 0 : (_api$showDiff2 = api.showDiff) === null || _api$showDiff2 === void 0 ? void 0 : (_api$showDiff2$comman = _api$showDiff2.commands) === null || _api$showDiff2$comman === void 0 ? void 0 : _api$showDiff2$comman.hideDiff);
84
78
  }
85
- });
86
- return decoration.add(tr.doc, decorations);
87
- }
79
+ }
80
+ };
88
81
  }
89
82
  });
90
83
  };
@@ -1,33 +1,49 @@
1
+ import React from 'react';
1
2
  import { createTrackChangesPlugin, trackChangesPluginKey } from './pm-plugins/main';
2
3
  import { TOGGLE_TRACK_CHANGES_ACTION as ACTION } from './pm-plugins/types';
3
- export const trackChangesPlugin = () => ({
4
- name: 'trackChanges',
5
- pmPlugins() {
6
- return [{
7
- name: 'trackChangesPlugin',
8
- plugin: createTrackChangesPlugin
9
- }];
10
- },
11
- commands: {
12
- toggleChanges: ({
13
- tr
14
- }) => {
15
- return tr.setMeta(trackChangesPluginKey, {
16
- action: ACTION.TOGGLE_TRACK_CHANGES
17
- });
18
- }
19
- },
20
- getSharedState: editorState => {
21
- var _trackChangesPluginKe, _trackChangesPluginKe2;
22
- if (!editorState) {
4
+ import { TrackChangesToolbarButton } from './ui/TrackChangesToolbarButton';
5
+ export const trackChangesPlugin = ({
6
+ api
7
+ }) => {
8
+ var _api$primaryToolbar, _api$primaryToolbar$a;
9
+ const primaryToolbarComponent = () => {
10
+ return /*#__PURE__*/React.createElement(TrackChangesToolbarButton, {
11
+ api: api
12
+ });
13
+ };
14
+ api === null || api === void 0 ? void 0 : (_api$primaryToolbar = api.primaryToolbar) === null || _api$primaryToolbar === void 0 ? void 0 : (_api$primaryToolbar$a = _api$primaryToolbar.actions) === null || _api$primaryToolbar$a === void 0 ? void 0 : _api$primaryToolbar$a.registerComponent({
15
+ name: 'trackChanges',
16
+ component: primaryToolbarComponent
17
+ });
18
+ return {
19
+ name: 'trackChanges',
20
+ pmPlugins() {
21
+ return [{
22
+ name: 'trackChangesPlugin',
23
+ plugin: () => createTrackChangesPlugin(api)
24
+ }];
25
+ },
26
+ commands: {
27
+ toggleChanges: ({
28
+ tr
29
+ }) => {
30
+ return tr.setMeta(trackChangesPluginKey, {
31
+ action: ACTION.TOGGLE_TRACK_CHANGES
32
+ });
33
+ }
34
+ },
35
+ getSharedState: editorState => {
36
+ var _trackChangesPluginKe, _trackChangesPluginKe2;
37
+ if (!editorState) {
38
+ return {
39
+ isDisplayingChanges: false,
40
+ isShowDiffAvailable: false
41
+ };
42
+ }
23
43
  return {
24
- isDisplayingChanges: false,
25
- isShowDiffAvailable: false
44
+ isDisplayingChanges: Boolean((_trackChangesPluginKe = trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe === void 0 ? void 0 : _trackChangesPluginKe.shouldChangesBeDisplayed),
45
+ isShowDiffAvailable: Boolean((_trackChangesPluginKe2 = trackChangesPluginKey.getState(editorState)) === null || _trackChangesPluginKe2 === void 0 ? void 0 : _trackChangesPluginKe2.isShowDiffAvailable)
26
46
  };
27
47
  }
28
- return {
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)
31
- };
32
- }
33
- });
48
+ };
49
+ };