@atlaskit/editor-plugin-show-diff 0.0.3 → 0.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-show-diff
2
2
 
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [`941fdc429d140`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/941fdc429d140) -
8
+ Show formatting changes in the diff
9
+ - Updated dependencies
10
+
11
+ ## 0.1.0
12
+
13
+ ### Minor Changes
14
+
15
+ - [`81ec1e909620a`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/81ec1e909620a) -
16
+ [EDITOR-1206] Have `editor-plugin-show-diff` expose if the plugin is displaying the diff.
17
+ Deprecate the state in `editor-plugin-track-changes` as it depends on `editor-plugin-show-diff`
18
+ and it's better to have the state in the plugin that actually shows the diff
19
+
3
20
  ## 0.0.3
4
21
 
5
22
  ### Patch Changes
@@ -0,0 +1,27 @@
1
+ {
2
+ "extends": "../../../../tsconfig.entry-points.passionfruit.json",
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "outDir": "../../../../../passionfruit/tsDist/@atlaskit__editor-plugin-show-diff/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-passionfruit/tsconfig.json"
22
+ },
23
+ {
24
+ "path": "../../editor-common/afm-passionfruit/tsconfig.json"
25
+ }
26
+ ]
27
+ }
@@ -0,0 +1,27 @@
1
+ {
2
+ "extends": "../../../../tsconfig.entry-points.volt.json",
3
+ "compilerOptions": {
4
+ "target": "es5",
5
+ "outDir": "../../../../../volt/tsDist/@atlaskit__editor-plugin-show-diff/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-volt/tsconfig.json"
22
+ },
23
+ {
24
+ "path": "../../editor-common/afm-volt/tsconfig.json"
25
+ }
26
+ ]
27
+ }
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.showDiffPluginKey = exports.createPlugin = void 0;
8
- var _readOnlyError2 = _interopRequireDefault(require("@babel/runtime/helpers/readOnlyError"));
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
9
  var _prosemirrorChangeset = require("prosemirror-changeset");
10
10
  var _processRawValue = require("@atlaskit/editor-common/process-raw-value");
11
11
  var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
@@ -13,81 +13,121 @@ var _state = require("@atlaskit/editor-prosemirror/state");
13
13
  var _transform = require("@atlaskit/editor-prosemirror/transform");
14
14
  var _view = require("@atlaskit/editor-prosemirror/view");
15
15
  var _decorations = require("./decorations");
16
+ var _markDecorations = require("./markDecorations");
17
+ 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
+ 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; }
16
19
  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; } } }; }
17
20
  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; } }
18
- 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; }
21
+ 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; } // eslint-disable-next-line @atlassian/tangerine/import/entry-points
22
+ var calculateDecorations = function calculateDecorations(_ref) {
23
+ var state = _ref.state,
24
+ pluginState = _ref.pluginState;
25
+ var originalDoc = pluginState.originalDoc,
26
+ steps = pluginState.steps;
27
+ if (!originalDoc || !pluginState.isDisplayingChanges) {
28
+ return _view.DecorationSet.empty;
29
+ }
30
+ var tr = state.tr;
31
+ var steppedDoc = originalDoc;
32
+ var changeset = _prosemirrorChangeset.ChangeSet.create(originalDoc);
33
+ var _iterator = _createForOfIteratorHelper(steps),
34
+ _step;
35
+ try {
36
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
37
+ var step = _step.value;
38
+ var result = step.apply(steppedDoc);
39
+ if (result.failed === null && result.doc) {
40
+ steppedDoc = result.doc;
41
+ changeset = changeset.addSteps(steppedDoc, [step.getMap()], tr.doc);
42
+ }
43
+ }
44
+ } catch (err) {
45
+ _iterator.e(err);
46
+ } finally {
47
+ _iterator.f();
48
+ }
49
+ if (!steppedDoc.eq(tr.doc)) {
50
+ return _view.DecorationSet.empty;
51
+ }
52
+ var changes = (0, _prosemirrorChangeset.simplifyChanges)(changeset.changes, tr.doc);
53
+ var decorations = [];
54
+ changes.forEach(function (change) {
55
+ if (change.inserted.length > 0) {
56
+ decorations.push((0, _decorations.createInlineChangedDecoration)(change));
57
+ }
58
+ if (change.deleted.length > 0) {
59
+ decorations.push((0, _decorations.createDeletedContentDecoration)({
60
+ change: change,
61
+ doc: originalDoc,
62
+ tr: tr
63
+ }));
64
+ }
65
+ });
66
+ (0, _markDecorations.getMarkChangeRanges)(steps).forEach(function (change) {
67
+ decorations.push((0, _decorations.createInlineChangedDecoration)(change));
68
+ });
69
+ return _view.DecorationSet.empty.add(tr.doc, decorations);
70
+ };
19
71
  var showDiffPluginKey = exports.showDiffPluginKey = new _state.PluginKey('showDiffPlugin');
20
72
  var createPlugin = exports.createPlugin = function createPlugin(config) {
21
73
  return new _safePlugin.SafePlugin({
22
74
  key: showDiffPluginKey,
23
75
  state: {
24
76
  init: function init(_, state) {
25
- var _config$steps;
77
+ var _config$steps, _config$steps2;
26
78
  var schema = state.schema;
79
+ var isDisplayingChanges = ((_config$steps = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps !== void 0 ? _config$steps : []).length > 0;
27
80
  return {
28
- steps: ((_config$steps = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps !== void 0 ? _config$steps : []).map(function (step) {
81
+ steps: ((_config$steps2 = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps2 !== void 0 ? _config$steps2 : []).map(function (step) {
29
82
  return _transform.Step.fromJSON(schema, step);
30
83
  }),
31
- originalDoc: config !== null && config !== void 0 && config.originalDoc ? (0, _processRawValue.processRawValue)(state.schema, config.originalDoc) : undefined
84
+ originalDoc: config !== null && config !== void 0 && config.originalDoc ? (0, _processRawValue.processRawValue)(state.schema, config.originalDoc) : undefined,
85
+ decorations: calculateDecorations({
86
+ state: state,
87
+ pluginState: {
88
+ steps: [],
89
+ originalDoc: config !== null && config !== void 0 && config.originalDoc ? (0, _processRawValue.processRawValue)(state.schema, config.originalDoc) : undefined,
90
+ isDisplayingChanges: isDisplayingChanges
91
+ }
92
+ }),
93
+ isDisplayingChanges: isDisplayingChanges
32
94
  };
33
95
  },
34
- apply: function apply(tr, currentPluginState) {
96
+ apply: function apply(tr, currentPluginState, oldState, newState) {
35
97
  var meta = tr.getMeta(showDiffPluginKey);
98
+ var newPluginState = currentPluginState;
36
99
  if (meta) {
37
- return meta;
100
+ if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'SHOW_DIFF') {
101
+ newPluginState = _objectSpread(_objectSpread(_objectSpread({}, currentPluginState), meta), {}, {
102
+ isDisplayingChanges: true
103
+ });
104
+ } else if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'HIDE_DIFF') {
105
+ newPluginState = _objectSpread(_objectSpread(_objectSpread({}, currentPluginState), meta), {}, {
106
+ isDisplayingChanges: false
107
+ });
108
+ } else {
109
+ newPluginState = _objectSpread(_objectSpread({}, currentPluginState), meta);
110
+ }
38
111
  }
39
- return currentPluginState;
112
+
113
+ // Calculate and store decorations in state
114
+ var decorations = calculateDecorations({
115
+ state: newState,
116
+ pluginState: {
117
+ steps: newPluginState.steps,
118
+ originalDoc: newPluginState.originalDoc,
119
+ isDisplayingChanges: newPluginState.isDisplayingChanges
120
+ }
121
+ });
122
+ return _objectSpread(_objectSpread({}, newPluginState), {}, {
123
+ decorations: decorations
124
+ });
40
125
  }
41
126
  },
42
127
  props: {
43
128
  decorations: function decorations(state) {
44
129
  var pluginState = showDiffPluginKey.getState(state);
45
- if (!pluginState) {
46
- return undefined;
47
- }
48
- var originalDoc = pluginState.originalDoc,
49
- steps = pluginState.steps;
50
- if (!originalDoc) {
51
- return undefined;
52
- }
53
- var tr = state.tr;
54
- var steppedDoc = originalDoc;
55
- var changeset = _prosemirrorChangeset.ChangeSet.create(originalDoc);
56
- var _iterator = _createForOfIteratorHelper(steps),
57
- _step;
58
- try {
59
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
60
- var step = _step.value;
61
- var result = step.apply(steppedDoc);
62
- if (result.failed === null && result.doc) {
63
- steppedDoc = result.doc;
64
- changeset = changeset.addSteps(steppedDoc, [step.getMap()], tr.doc);
65
- }
66
- }
67
- } catch (err) {
68
- _iterator.e(err);
69
- } finally {
70
- _iterator.f();
71
- }
72
- if (!steppedDoc.eq(tr.doc)) {
73
- return undefined;
74
- }
75
- var changes = (0, _prosemirrorChangeset.simplifyChanges)(changeset.changes, tr.doc);
76
- var decoration = _view.DecorationSet.empty;
77
- var decorations = [];
78
- changes.forEach(function (change) {
79
- if (change.inserted.length > 0) {
80
- decorations.push((0, _decorations.createInlineChangedDecoration)(change));
81
- }
82
- if (change.deleted.length > 0) {
83
- decorations.push((0, _decorations.createDeletedContentDecoration)({
84
- change: change,
85
- doc: originalDoc,
86
- tr: tr
87
- }));
88
- }
89
- });
90
- return decoration.add(tr.doc, decorations);
130
+ return pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorations;
91
131
  }
92
132
  }
93
133
  });
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getMarkChangeRanges = void 0;
7
+ var _transform = require("@atlaskit/editor-prosemirror/transform");
8
+ var filterUndefined = function filterUndefined(x) {
9
+ return !!x;
10
+ };
11
+ var getMarkChangeRanges = exports.getMarkChangeRanges = function getMarkChangeRanges(steps) {
12
+ return steps.map(function (step) {
13
+ if (step instanceof _transform.AddMarkStep || step instanceof _transform.RemoveMarkStep) {
14
+ return {
15
+ fromB: step.from,
16
+ toB: step.to
17
+ };
18
+ }
19
+ return undefined;
20
+ }).filter(filterUndefined);
21
+ };
@@ -1,10 +1,14 @@
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.showDiffPlugin = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
7
9
  var _main = require("./pm-plugins/main");
10
+ 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; }
11
+ 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; }
8
12
  var showDiffPlugin = exports.showDiffPlugin = function showDiffPlugin(_ref) {
9
13
  var api = _ref.api,
10
14
  config = _ref.config;
@@ -14,13 +18,16 @@ var showDiffPlugin = exports.showDiffPlugin = function showDiffPlugin(_ref) {
14
18
  showDiff: function showDiff(params) {
15
19
  return function (_ref2) {
16
20
  var tr = _ref2.tr;
17
- return tr.setMeta(_main.showDiffPluginKey, params);
21
+ return tr.setMeta(_main.showDiffPluginKey, _objectSpread(_objectSpread({}, params), {}, {
22
+ action: 'SHOW_DIFF'
23
+ }));
18
24
  };
19
25
  },
20
26
  hideDiff: function hideDiff(_ref3) {
21
27
  var tr = _ref3.tr;
22
28
  return tr.setMeta(_main.showDiffPluginKey, {
23
- steps: []
29
+ steps: [],
30
+ action: 'HIDE_DIFF'
24
31
  });
25
32
  }
26
33
  },
@@ -31,6 +38,19 @@ var showDiffPlugin = exports.showDiffPlugin = function showDiffPlugin(_ref) {
31
38
  return (0, _main.createPlugin)(config);
32
39
  }
33
40
  }];
41
+ },
42
+ getSharedState: function getSharedState(editorState) {
43
+ var _pluginState$decorati;
44
+ if (!editorState) {
45
+ return {
46
+ isDisplayingChanges: false
47
+ };
48
+ }
49
+ var pluginState = _main.showDiffPluginKey.getState(editorState);
50
+ var decorationCount = (pluginState === null || pluginState === void 0 || (_pluginState$decorati = pluginState.decorations) === null || _pluginState$decorati === void 0 ? void 0 : _pluginState$decorati.find()) || [];
51
+ return {
52
+ isDisplayingChanges: decorationCount.length > 0
53
+ };
34
54
  }
35
55
  };
36
56
  };
@@ -1,3 +1,4 @@
1
+ // eslint-disable-next-line @atlassian/tangerine/import/entry-points
1
2
  import { ChangeSet, simplifyChanges } from 'prosemirror-changeset';
2
3
  import { processRawValue } from '@atlaskit/editor-common/process-raw-value';
3
4
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
@@ -5,71 +6,118 @@ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
5
6
  import { Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform';
6
7
  import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
7
8
  import { createInlineChangedDecoration, createDeletedContentDecoration } from './decorations';
9
+ import { getMarkChangeRanges } from './markDecorations';
10
+ const calculateDecorations = ({
11
+ state,
12
+ pluginState
13
+ }) => {
14
+ const {
15
+ originalDoc,
16
+ steps
17
+ } = pluginState;
18
+ if (!originalDoc || !pluginState.isDisplayingChanges) {
19
+ return DecorationSet.empty;
20
+ }
21
+ const {
22
+ tr
23
+ } = state;
24
+ let steppedDoc = originalDoc;
25
+ let changeset = ChangeSet.create(originalDoc);
26
+ for (const step of steps) {
27
+ const result = step.apply(steppedDoc);
28
+ if (result.failed === null && result.doc) {
29
+ steppedDoc = result.doc;
30
+ changeset = changeset.addSteps(steppedDoc, [step.getMap()], tr.doc);
31
+ }
32
+ }
33
+ if (!steppedDoc.eq(tr.doc)) {
34
+ return DecorationSet.empty;
35
+ }
36
+ const changes = simplifyChanges(changeset.changes, tr.doc);
37
+ const decorations = [];
38
+ changes.forEach(change => {
39
+ if (change.inserted.length > 0) {
40
+ decorations.push(createInlineChangedDecoration(change));
41
+ }
42
+ if (change.deleted.length > 0) {
43
+ decorations.push(createDeletedContentDecoration({
44
+ change,
45
+ doc: originalDoc,
46
+ tr
47
+ }));
48
+ }
49
+ });
50
+ getMarkChangeRanges(steps).forEach(change => {
51
+ decorations.push(createInlineChangedDecoration(change));
52
+ });
53
+ return DecorationSet.empty.add(tr.doc, decorations);
54
+ };
8
55
  export const showDiffPluginKey = new PluginKey('showDiffPlugin');
9
56
  export const createPlugin = config => {
10
57
  return new SafePlugin({
11
58
  key: showDiffPluginKey,
12
59
  state: {
13
60
  init(_, state) {
14
- var _config$steps;
61
+ var _config$steps, _config$steps2;
15
62
  const schema = state.schema;
63
+ const isDisplayingChanges = ((_config$steps = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps !== void 0 ? _config$steps : []).length > 0;
16
64
  return {
17
- steps: ((_config$steps = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps !== void 0 ? _config$steps : []).map(step => ProseMirrorStep.fromJSON(schema, step)),
18
- originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined
65
+ steps: ((_config$steps2 = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps2 !== void 0 ? _config$steps2 : []).map(step => ProseMirrorStep.fromJSON(schema, step)),
66
+ originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined,
67
+ decorations: calculateDecorations({
68
+ state,
69
+ pluginState: {
70
+ steps: [],
71
+ originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined,
72
+ isDisplayingChanges
73
+ }
74
+ }),
75
+ isDisplayingChanges
19
76
  };
20
77
  },
21
- apply: (tr, currentPluginState) => {
78
+ apply: (tr, currentPluginState, oldState, newState) => {
22
79
  const meta = tr.getMeta(showDiffPluginKey);
80
+ let newPluginState = currentPluginState;
23
81
  if (meta) {
24
- return meta;
82
+ if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'SHOW_DIFF') {
83
+ newPluginState = {
84
+ ...currentPluginState,
85
+ ...meta,
86
+ isDisplayingChanges: true
87
+ };
88
+ } else if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'HIDE_DIFF') {
89
+ newPluginState = {
90
+ ...currentPluginState,
91
+ ...meta,
92
+ isDisplayingChanges: false
93
+ };
94
+ } else {
95
+ newPluginState = {
96
+ ...currentPluginState,
97
+ ...meta
98
+ };
99
+ }
25
100
  }
26
- return currentPluginState;
101
+
102
+ // Calculate and store decorations in state
103
+ const decorations = calculateDecorations({
104
+ state: newState,
105
+ pluginState: {
106
+ steps: newPluginState.steps,
107
+ originalDoc: newPluginState.originalDoc,
108
+ isDisplayingChanges: newPluginState.isDisplayingChanges
109
+ }
110
+ });
111
+ return {
112
+ ...newPluginState,
113
+ decorations
114
+ };
27
115
  }
28
116
  },
29
117
  props: {
30
118
  decorations: state => {
31
119
  const pluginState = showDiffPluginKey.getState(state);
32
- if (!pluginState) {
33
- return undefined;
34
- }
35
- const {
36
- originalDoc,
37
- steps
38
- } = pluginState;
39
- if (!originalDoc) {
40
- return undefined;
41
- }
42
- const {
43
- tr
44
- } = state;
45
- let steppedDoc = originalDoc;
46
- let changeset = ChangeSet.create(originalDoc);
47
- for (const step of steps) {
48
- const result = step.apply(steppedDoc);
49
- if (result.failed === null && result.doc) {
50
- steppedDoc = result.doc;
51
- changeset = changeset.addSteps(steppedDoc, [step.getMap()], tr.doc);
52
- }
53
- }
54
- if (!steppedDoc.eq(tr.doc)) {
55
- return undefined;
56
- }
57
- const changes = simplifyChanges(changeset.changes, tr.doc);
58
- const decoration = DecorationSet.empty;
59
- const decorations = [];
60
- changes.forEach(change => {
61
- if (change.inserted.length > 0) {
62
- decorations.push(createInlineChangedDecoration(change));
63
- }
64
- if (change.deleted.length > 0) {
65
- decorations.push(createDeletedContentDecoration({
66
- change,
67
- doc: originalDoc,
68
- tr
69
- }));
70
- }
71
- });
72
- return decoration.add(tr.doc, decorations);
120
+ return pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorations;
73
121
  }
74
122
  }
75
123
  });
@@ -0,0 +1,13 @@
1
+ import { AddMarkStep, RemoveMarkStep } from '@atlaskit/editor-prosemirror/transform';
2
+ const filterUndefined = x => !!x;
3
+ export const getMarkChangeRanges = steps => {
4
+ return steps.map(step => {
5
+ if (step instanceof AddMarkStep || step instanceof RemoveMarkStep) {
6
+ return {
7
+ fromB: step.from,
8
+ toB: step.to
9
+ };
10
+ }
11
+ return undefined;
12
+ }).filter(filterUndefined);
13
+ };
@@ -8,13 +8,17 @@ export const showDiffPlugin = ({
8
8
  showDiff: params => ({
9
9
  tr
10
10
  }) => {
11
- return tr.setMeta(showDiffPluginKey, params);
11
+ return tr.setMeta(showDiffPluginKey, {
12
+ ...params,
13
+ action: 'SHOW_DIFF'
14
+ });
12
15
  },
13
16
  hideDiff: ({
14
17
  tr
15
18
  }) => {
16
19
  return tr.setMeta(showDiffPluginKey, {
17
- steps: []
20
+ steps: [],
21
+ action: 'HIDE_DIFF'
18
22
  });
19
23
  }
20
24
  },
@@ -23,5 +27,18 @@ export const showDiffPlugin = ({
23
27
  name: 'showDiffPlugin',
24
28
  plugin: () => createPlugin(config)
25
29
  }];
30
+ },
31
+ getSharedState: editorState => {
32
+ var _pluginState$decorati;
33
+ if (!editorState) {
34
+ return {
35
+ isDisplayingChanges: false
36
+ };
37
+ }
38
+ const pluginState = showDiffPluginKey.getState(editorState);
39
+ const decorationCount = (pluginState === null || pluginState === void 0 ? void 0 : (_pluginState$decorati = pluginState.decorations) === null || _pluginState$decorati === void 0 ? void 0 : _pluginState$decorati.find()) || [];
40
+ return {
41
+ isDisplayingChanges: decorationCount.length > 0
42
+ };
26
43
  }
27
44
  });
@@ -1,7 +1,10 @@
1
- import _readOnlyError from "@babel/runtime/helpers/readOnlyError";
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
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
+ 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; }
2
4
  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; } } }; }
3
5
  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; } }
4
6
  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; }
7
+ // eslint-disable-next-line @atlassian/tangerine/import/entry-points
5
8
  import { ChangeSet, simplifyChanges } from 'prosemirror-changeset';
6
9
  import { processRawValue } from '@atlaskit/editor-common/process-raw-value';
7
10
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
@@ -9,78 +12,116 @@ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
9
12
  import { Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform';
10
13
  import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
11
14
  import { createInlineChangedDecoration, createDeletedContentDecoration } from './decorations';
15
+ import { getMarkChangeRanges } from './markDecorations';
16
+ var calculateDecorations = function calculateDecorations(_ref) {
17
+ var state = _ref.state,
18
+ pluginState = _ref.pluginState;
19
+ var originalDoc = pluginState.originalDoc,
20
+ steps = pluginState.steps;
21
+ if (!originalDoc || !pluginState.isDisplayingChanges) {
22
+ return DecorationSet.empty;
23
+ }
24
+ var tr = state.tr;
25
+ var steppedDoc = originalDoc;
26
+ var changeset = ChangeSet.create(originalDoc);
27
+ var _iterator = _createForOfIteratorHelper(steps),
28
+ _step;
29
+ try {
30
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
31
+ var step = _step.value;
32
+ var result = step.apply(steppedDoc);
33
+ if (result.failed === null && result.doc) {
34
+ steppedDoc = result.doc;
35
+ changeset = changeset.addSteps(steppedDoc, [step.getMap()], tr.doc);
36
+ }
37
+ }
38
+ } catch (err) {
39
+ _iterator.e(err);
40
+ } finally {
41
+ _iterator.f();
42
+ }
43
+ if (!steppedDoc.eq(tr.doc)) {
44
+ return DecorationSet.empty;
45
+ }
46
+ var changes = simplifyChanges(changeset.changes, tr.doc);
47
+ var decorations = [];
48
+ changes.forEach(function (change) {
49
+ if (change.inserted.length > 0) {
50
+ decorations.push(createInlineChangedDecoration(change));
51
+ }
52
+ if (change.deleted.length > 0) {
53
+ decorations.push(createDeletedContentDecoration({
54
+ change: change,
55
+ doc: originalDoc,
56
+ tr: tr
57
+ }));
58
+ }
59
+ });
60
+ getMarkChangeRanges(steps).forEach(function (change) {
61
+ decorations.push(createInlineChangedDecoration(change));
62
+ });
63
+ return DecorationSet.empty.add(tr.doc, decorations);
64
+ };
12
65
  export var showDiffPluginKey = new PluginKey('showDiffPlugin');
13
66
  export var createPlugin = function createPlugin(config) {
14
67
  return new SafePlugin({
15
68
  key: showDiffPluginKey,
16
69
  state: {
17
70
  init: function init(_, state) {
18
- var _config$steps;
71
+ var _config$steps, _config$steps2;
19
72
  var schema = state.schema;
73
+ var isDisplayingChanges = ((_config$steps = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps !== void 0 ? _config$steps : []).length > 0;
20
74
  return {
21
- steps: ((_config$steps = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps !== void 0 ? _config$steps : []).map(function (step) {
75
+ steps: ((_config$steps2 = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps2 !== void 0 ? _config$steps2 : []).map(function (step) {
22
76
  return ProseMirrorStep.fromJSON(schema, step);
23
77
  }),
24
- originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined
78
+ originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined,
79
+ decorations: calculateDecorations({
80
+ state: state,
81
+ pluginState: {
82
+ steps: [],
83
+ originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined,
84
+ isDisplayingChanges: isDisplayingChanges
85
+ }
86
+ }),
87
+ isDisplayingChanges: isDisplayingChanges
25
88
  };
26
89
  },
27
- apply: function apply(tr, currentPluginState) {
90
+ apply: function apply(tr, currentPluginState, oldState, newState) {
28
91
  var meta = tr.getMeta(showDiffPluginKey);
92
+ var newPluginState = currentPluginState;
29
93
  if (meta) {
30
- return meta;
94
+ if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'SHOW_DIFF') {
95
+ newPluginState = _objectSpread(_objectSpread(_objectSpread({}, currentPluginState), meta), {}, {
96
+ isDisplayingChanges: true
97
+ });
98
+ } else if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'HIDE_DIFF') {
99
+ newPluginState = _objectSpread(_objectSpread(_objectSpread({}, currentPluginState), meta), {}, {
100
+ isDisplayingChanges: false
101
+ });
102
+ } else {
103
+ newPluginState = _objectSpread(_objectSpread({}, currentPluginState), meta);
104
+ }
31
105
  }
32
- return currentPluginState;
106
+
107
+ // Calculate and store decorations in state
108
+ var decorations = calculateDecorations({
109
+ state: newState,
110
+ pluginState: {
111
+ steps: newPluginState.steps,
112
+ originalDoc: newPluginState.originalDoc,
113
+ isDisplayingChanges: newPluginState.isDisplayingChanges
114
+ }
115
+ });
116
+ return _objectSpread(_objectSpread({}, newPluginState), {}, {
117
+ decorations: decorations
118
+ });
33
119
  }
34
120
  },
35
121
  props: {
36
122
  decorations: function decorations(state) {
37
123
  var pluginState = showDiffPluginKey.getState(state);
38
- if (!pluginState) {
39
- return undefined;
40
- }
41
- var originalDoc = pluginState.originalDoc,
42
- steps = pluginState.steps;
43
- if (!originalDoc) {
44
- return undefined;
45
- }
46
- var tr = state.tr;
47
- var steppedDoc = originalDoc;
48
- var changeset = ChangeSet.create(originalDoc);
49
- var _iterator = _createForOfIteratorHelper(steps),
50
- _step;
51
- try {
52
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
53
- var step = _step.value;
54
- var result = step.apply(steppedDoc);
55
- if (result.failed === null && result.doc) {
56
- steppedDoc = result.doc;
57
- changeset = changeset.addSteps(steppedDoc, [step.getMap()], tr.doc);
58
- }
59
- }
60
- } catch (err) {
61
- _iterator.e(err);
62
- } finally {
63
- _iterator.f();
64
- }
65
- if (!steppedDoc.eq(tr.doc)) {
66
- return undefined;
67
- }
68
- var changes = simplifyChanges(changeset.changes, tr.doc);
69
- var decoration = DecorationSet.empty;
70
- var decorations = [];
71
- changes.forEach(function (change) {
72
- if (change.inserted.length > 0) {
73
- decorations.push(createInlineChangedDecoration(change));
74
- }
75
- if (change.deleted.length > 0) {
76
- decorations.push(createDeletedContentDecoration({
77
- change: change,
78
- doc: originalDoc,
79
- tr: tr
80
- }));
81
- }
82
- });
83
- return decoration.add(tr.doc, decorations);
124
+ return pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorations;
84
125
  }
85
126
  }
86
127
  });
@@ -0,0 +1,15 @@
1
+ import { AddMarkStep, RemoveMarkStep } from '@atlaskit/editor-prosemirror/transform';
2
+ var filterUndefined = function filterUndefined(x) {
3
+ return !!x;
4
+ };
5
+ export var getMarkChangeRanges = function getMarkChangeRanges(steps) {
6
+ return steps.map(function (step) {
7
+ if (step instanceof AddMarkStep || step instanceof RemoveMarkStep) {
8
+ return {
9
+ fromB: step.from,
10
+ toB: step.to
11
+ };
12
+ }
13
+ return undefined;
14
+ }).filter(filterUndefined);
15
+ };
@@ -1,3 +1,6 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
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
+ 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; }
1
4
  import { createPlugin, showDiffPluginKey } from './pm-plugins/main';
2
5
  export var showDiffPlugin = function showDiffPlugin(_ref) {
3
6
  var api = _ref.api,
@@ -8,13 +11,16 @@ export var showDiffPlugin = function showDiffPlugin(_ref) {
8
11
  showDiff: function showDiff(params) {
9
12
  return function (_ref2) {
10
13
  var tr = _ref2.tr;
11
- return tr.setMeta(showDiffPluginKey, params);
14
+ return tr.setMeta(showDiffPluginKey, _objectSpread(_objectSpread({}, params), {}, {
15
+ action: 'SHOW_DIFF'
16
+ }));
12
17
  };
13
18
  },
14
19
  hideDiff: function hideDiff(_ref3) {
15
20
  var tr = _ref3.tr;
16
21
  return tr.setMeta(showDiffPluginKey, {
17
- steps: []
22
+ steps: [],
23
+ action: 'HIDE_DIFF'
18
24
  });
19
25
  }
20
26
  },
@@ -25,6 +31,19 @@ export var showDiffPlugin = function showDiffPlugin(_ref) {
25
31
  return createPlugin(config);
26
32
  }
27
33
  }];
34
+ },
35
+ getSharedState: function getSharedState(editorState) {
36
+ var _pluginState$decorati;
37
+ if (!editorState) {
38
+ return {
39
+ isDisplayingChanges: false
40
+ };
41
+ }
42
+ var pluginState = showDiffPluginKey.getState(editorState);
43
+ var decorationCount = (pluginState === null || pluginState === void 0 || (_pluginState$decorati = pluginState.decorations) === null || _pluginState$decorati === void 0 ? void 0 : _pluginState$decorati.find()) || [];
44
+ return {
45
+ isDisplayingChanges: decorationCount.length > 0
46
+ };
28
47
  }
29
48
  };
30
49
  };
@@ -8,7 +8,10 @@ import { Decoration } from '@atlaskit/editor-prosemirror/view';
8
8
  * @param change Changeset "change" containing information about the change content + range
9
9
  * @returns Prosemirror inline decoration
10
10
  */
11
- export declare const createInlineChangedDecoration: (change: Change) => Decoration;
11
+ export declare const createInlineChangedDecoration: (change: {
12
+ fromB: number;
13
+ toB: number;
14
+ }) => Decoration;
12
15
  interface DeletedContentDecorationProps {
13
16
  change: Change;
14
17
  doc: PMNode;
@@ -2,11 +2,14 @@ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
2
  import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
3
3
  import { PluginKey } from '@atlaskit/editor-prosemirror/state';
4
4
  import { Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform';
5
- import type { DiffParams } from '../showDiffPluginType';
5
+ import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
6
+ import { type DiffParams } from '../showDiffPluginType';
6
7
  export declare const showDiffPluginKey: PluginKey<ShowDiffPluginState>;
7
8
  type ShowDiffPluginState = {
8
9
  steps: ProseMirrorStep[];
9
10
  originalDoc: PMNode | undefined;
11
+ decorations: DecorationSet;
12
+ isDisplayingChanges: boolean;
10
13
  };
11
14
  export declare const createPlugin: (config: DiffParams | undefined) => SafePlugin<ShowDiffPluginState>;
12
15
  export {};
@@ -0,0 +1,7 @@
1
+ import { type Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform';
2
+ type StepRange = {
3
+ fromB: number;
4
+ toB: number;
5
+ };
6
+ export declare const getMarkChangeRanges: (steps: ProseMirrorStep[]) => StepRange[];
7
+ export {};
@@ -17,10 +17,18 @@ export type PMDiffParams = {
17
17
  steps: Step[];
18
18
  originalDoc: Node;
19
19
  };
20
+ export type ACTION = 'SHOW_DIFF' | 'HIDE_DIFF';
20
21
  export type ShowDiffPlugin = NextEditorPlugin<'showDiff', {
21
22
  pluginConfiguration: DiffParams | undefined;
22
23
  commands: {
23
24
  showDiff: (config: PMDiffParams) => EditorCommand;
24
25
  hideDiff: EditorCommand;
25
26
  };
27
+ sharedState: {
28
+ /**
29
+ * Whether the show diff feature is currently displaying changes.
30
+ * Defaults to false.
31
+ */
32
+ isDisplayingChanges: boolean;
33
+ };
26
34
  }>;
@@ -8,7 +8,10 @@ import { Decoration } from '@atlaskit/editor-prosemirror/view';
8
8
  * @param change Changeset "change" containing information about the change content + range
9
9
  * @returns Prosemirror inline decoration
10
10
  */
11
- export declare const createInlineChangedDecoration: (change: Change) => Decoration;
11
+ export declare const createInlineChangedDecoration: (change: {
12
+ fromB: number;
13
+ toB: number;
14
+ }) => Decoration;
12
15
  interface DeletedContentDecorationProps {
13
16
  change: Change;
14
17
  doc: PMNode;
@@ -2,11 +2,14 @@ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
2
  import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
3
3
  import { PluginKey } from '@atlaskit/editor-prosemirror/state';
4
4
  import { Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform';
5
- import type { DiffParams } from '../showDiffPluginType';
5
+ import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
6
+ import { type DiffParams } from '../showDiffPluginType';
6
7
  export declare const showDiffPluginKey: PluginKey<ShowDiffPluginState>;
7
8
  type ShowDiffPluginState = {
8
9
  steps: ProseMirrorStep[];
9
10
  originalDoc: PMNode | undefined;
11
+ decorations: DecorationSet;
12
+ isDisplayingChanges: boolean;
10
13
  };
11
14
  export declare const createPlugin: (config: DiffParams | undefined) => SafePlugin<ShowDiffPluginState>;
12
15
  export {};
@@ -0,0 +1,7 @@
1
+ import { type Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform';
2
+ type StepRange = {
3
+ fromB: number;
4
+ toB: number;
5
+ };
6
+ export declare const getMarkChangeRanges: (steps: ProseMirrorStep[]) => StepRange[];
7
+ export {};
@@ -17,10 +17,18 @@ export type PMDiffParams = {
17
17
  steps: Step[];
18
18
  originalDoc: Node;
19
19
  };
20
+ export type ACTION = 'SHOW_DIFF' | 'HIDE_DIFF';
20
21
  export type ShowDiffPlugin = NextEditorPlugin<'showDiff', {
21
22
  pluginConfiguration: DiffParams | undefined;
22
23
  commands: {
23
24
  showDiff: (config: PMDiffParams) => EditorCommand;
24
25
  hideDiff: EditorCommand;
25
26
  };
27
+ sharedState: {
28
+ /**
29
+ * Whether the show diff feature is currently displaying changes.
30
+ * Defaults to false.
31
+ */
32
+ isDisplayingChanges: boolean;
33
+ };
26
34
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-show-diff",
3
- "version": "0.0.3",
3
+ "version": "0.1.1",
4
4
  "description": "ShowDiff plugin for @atlaskit/editor-core",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -37,7 +37,7 @@
37
37
  "prosemirror-changeset": "^2.2.1"
38
38
  },
39
39
  "peerDependencies": {
40
- "@atlaskit/editor-common": "^107.19.0",
40
+ "@atlaskit/editor-common": "^107.23.0",
41
41
  "react": "^18.2.0"
42
42
  },
43
43
  "techstack": {