@atlaskit/editor-plugin-show-diff 0.0.3 → 0.1.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 +9 -0
- package/dist/cjs/pm-plugins/main.js +84 -52
- package/dist/cjs/showDiffPlugin.js +22 -2
- package/dist/es2019/pm-plugins/main.js +85 -45
- package/dist/es2019/showDiffPlugin.js +19 -2
- package/dist/esm/pm-plugins/main.js +84 -51
- package/dist/esm/showDiffPlugin.js +21 -2
- package/dist/types/pm-plugins/main.d.ts +3 -1
- package/dist/types/showDiffPluginType.d.ts +8 -0
- package/dist/types-ts4.5/pm-plugins/main.d.ts +3 -1
- package/dist/types-ts4.5/showDiffPluginType.d.ts +8 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @atlaskit/editor-plugin-show-diff
|
|
2
2
|
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`81ec1e909620a`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/81ec1e909620a) -
|
|
8
|
+
[EDITOR-1206] Have `editor-plugin-show-diff` expose if the plugin is displaying the diff.
|
|
9
|
+
Deprecate the state in `editor-plugin-track-changes` as it depends on `editor-plugin-show-diff`
|
|
10
|
+
and it's better to have the state in the plugin that actually shows the diff
|
|
11
|
+
|
|
3
12
|
## 0.0.3
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.showDiffPluginKey = exports.createPlugin = void 0;
|
|
8
|
-
var
|
|
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,9 +13,57 @@ 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
|
+
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
|
+
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
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; } } }; }
|
|
17
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; } }
|
|
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; }
|
|
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; } // eslint-disable-next-line @atlassian/tangerine/import/entry-points
|
|
21
|
+
var calculateDecorations = function calculateDecorations(_ref) {
|
|
22
|
+
var state = _ref.state,
|
|
23
|
+
pluginState = _ref.pluginState;
|
|
24
|
+
var originalDoc = pluginState.originalDoc,
|
|
25
|
+
steps = pluginState.steps;
|
|
26
|
+
if (!originalDoc) {
|
|
27
|
+
return _view.DecorationSet.empty;
|
|
28
|
+
}
|
|
29
|
+
var tr = state.tr;
|
|
30
|
+
var steppedDoc = originalDoc;
|
|
31
|
+
var changeset = _prosemirrorChangeset.ChangeSet.create(originalDoc);
|
|
32
|
+
var _iterator = _createForOfIteratorHelper(steps),
|
|
33
|
+
_step;
|
|
34
|
+
try {
|
|
35
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
36
|
+
var step = _step.value;
|
|
37
|
+
var result = step.apply(steppedDoc);
|
|
38
|
+
if (result.failed === null && result.doc) {
|
|
39
|
+
steppedDoc = result.doc;
|
|
40
|
+
changeset = changeset.addSteps(steppedDoc, [step.getMap()], tr.doc);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} catch (err) {
|
|
44
|
+
_iterator.e(err);
|
|
45
|
+
} finally {
|
|
46
|
+
_iterator.f();
|
|
47
|
+
}
|
|
48
|
+
if (!steppedDoc.eq(tr.doc)) {
|
|
49
|
+
return _view.DecorationSet.empty;
|
|
50
|
+
}
|
|
51
|
+
var changes = (0, _prosemirrorChangeset.simplifyChanges)(changeset.changes, tr.doc);
|
|
52
|
+
var decorations = [];
|
|
53
|
+
changes.forEach(function (change) {
|
|
54
|
+
if (change.inserted.length > 0) {
|
|
55
|
+
decorations.push((0, _decorations.createInlineChangedDecoration)(change));
|
|
56
|
+
}
|
|
57
|
+
if (change.deleted.length > 0) {
|
|
58
|
+
decorations.push((0, _decorations.createDeletedContentDecoration)({
|
|
59
|
+
change: change,
|
|
60
|
+
doc: originalDoc,
|
|
61
|
+
tr: tr
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return _view.DecorationSet.empty.add(tr.doc, decorations);
|
|
66
|
+
};
|
|
19
67
|
var showDiffPluginKey = exports.showDiffPluginKey = new _state.PluginKey('showDiffPlugin');
|
|
20
68
|
var createPlugin = exports.createPlugin = function createPlugin(config) {
|
|
21
69
|
return new _safePlugin.SafePlugin({
|
|
@@ -28,66 +76,50 @@ var createPlugin = exports.createPlugin = function createPlugin(config) {
|
|
|
28
76
|
steps: ((_config$steps = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps !== void 0 ? _config$steps : []).map(function (step) {
|
|
29
77
|
return _transform.Step.fromJSON(schema, step);
|
|
30
78
|
}),
|
|
31
|
-
originalDoc: config !== null && config !== void 0 && config.originalDoc ? (0, _processRawValue.processRawValue)(state.schema, config.originalDoc) : undefined
|
|
79
|
+
originalDoc: config !== null && config !== void 0 && config.originalDoc ? (0, _processRawValue.processRawValue)(state.schema, config.originalDoc) : undefined,
|
|
80
|
+
decorations: calculateDecorations({
|
|
81
|
+
state: state,
|
|
82
|
+
pluginState: {
|
|
83
|
+
steps: [],
|
|
84
|
+
originalDoc: config !== null && config !== void 0 && config.originalDoc ? (0, _processRawValue.processRawValue)(state.schema, config.originalDoc) : undefined
|
|
85
|
+
}
|
|
86
|
+
})
|
|
32
87
|
};
|
|
33
88
|
},
|
|
34
|
-
apply: function apply(tr, currentPluginState) {
|
|
89
|
+
apply: function apply(tr, currentPluginState, oldState, newState) {
|
|
35
90
|
var meta = tr.getMeta(showDiffPluginKey);
|
|
91
|
+
var newPluginState = currentPluginState;
|
|
36
92
|
if (meta) {
|
|
37
|
-
|
|
93
|
+
if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'SHOW_DIFF') {
|
|
94
|
+
newPluginState = _objectSpread(_objectSpread(_objectSpread({}, currentPluginState), meta), {}, {
|
|
95
|
+
isDisplayingChanges: true
|
|
96
|
+
});
|
|
97
|
+
} else if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'HIDE_DIFF') {
|
|
98
|
+
newPluginState = _objectSpread(_objectSpread(_objectSpread({}, currentPluginState), meta), {}, {
|
|
99
|
+
isDisplayingChanges: false
|
|
100
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
newPluginState = _objectSpread(_objectSpread({}, currentPluginState), meta);
|
|
103
|
+
}
|
|
38
104
|
}
|
|
39
|
-
|
|
105
|
+
|
|
106
|
+
// Calculate and store decorations in state
|
|
107
|
+
var decorations = calculateDecorations({
|
|
108
|
+
state: newState,
|
|
109
|
+
pluginState: {
|
|
110
|
+
steps: newPluginState.steps,
|
|
111
|
+
originalDoc: newPluginState.originalDoc
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return _objectSpread(_objectSpread({}, newPluginState), {}, {
|
|
115
|
+
decorations: decorations
|
|
116
|
+
});
|
|
40
117
|
}
|
|
41
118
|
},
|
|
42
119
|
props: {
|
|
43
120
|
decorations: function decorations(state) {
|
|
44
121
|
var pluginState = showDiffPluginKey.getState(state);
|
|
45
|
-
|
|
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);
|
|
122
|
+
return pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorations;
|
|
91
123
|
}
|
|
92
124
|
}
|
|
93
125
|
});
|
|
@@ -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,6 +6,48 @@ 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
|
+
const calculateDecorations = ({
|
|
10
|
+
state,
|
|
11
|
+
pluginState
|
|
12
|
+
}) => {
|
|
13
|
+
const {
|
|
14
|
+
originalDoc,
|
|
15
|
+
steps
|
|
16
|
+
} = pluginState;
|
|
17
|
+
if (!originalDoc) {
|
|
18
|
+
return DecorationSet.empty;
|
|
19
|
+
}
|
|
20
|
+
const {
|
|
21
|
+
tr
|
|
22
|
+
} = state;
|
|
23
|
+
let steppedDoc = originalDoc;
|
|
24
|
+
let changeset = ChangeSet.create(originalDoc);
|
|
25
|
+
for (const step of steps) {
|
|
26
|
+
const result = step.apply(steppedDoc);
|
|
27
|
+
if (result.failed === null && result.doc) {
|
|
28
|
+
steppedDoc = result.doc;
|
|
29
|
+
changeset = changeset.addSteps(steppedDoc, [step.getMap()], tr.doc);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (!steppedDoc.eq(tr.doc)) {
|
|
33
|
+
return DecorationSet.empty;
|
|
34
|
+
}
|
|
35
|
+
const changes = simplifyChanges(changeset.changes, tr.doc);
|
|
36
|
+
const decorations = [];
|
|
37
|
+
changes.forEach(change => {
|
|
38
|
+
if (change.inserted.length > 0) {
|
|
39
|
+
decorations.push(createInlineChangedDecoration(change));
|
|
40
|
+
}
|
|
41
|
+
if (change.deleted.length > 0) {
|
|
42
|
+
decorations.push(createDeletedContentDecoration({
|
|
43
|
+
change,
|
|
44
|
+
doc: originalDoc,
|
|
45
|
+
tr
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return DecorationSet.empty.add(tr.doc, decorations);
|
|
50
|
+
};
|
|
8
51
|
export const showDiffPluginKey = new PluginKey('showDiffPlugin');
|
|
9
52
|
export const createPlugin = config => {
|
|
10
53
|
return new SafePlugin({
|
|
@@ -15,61 +58,58 @@ export const createPlugin = config => {
|
|
|
15
58
|
const schema = state.schema;
|
|
16
59
|
return {
|
|
17
60
|
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
|
|
61
|
+
originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined,
|
|
62
|
+
decorations: calculateDecorations({
|
|
63
|
+
state,
|
|
64
|
+
pluginState: {
|
|
65
|
+
steps: [],
|
|
66
|
+
originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined
|
|
67
|
+
}
|
|
68
|
+
})
|
|
19
69
|
};
|
|
20
70
|
},
|
|
21
|
-
apply: (tr, currentPluginState) => {
|
|
71
|
+
apply: (tr, currentPluginState, oldState, newState) => {
|
|
22
72
|
const meta = tr.getMeta(showDiffPluginKey);
|
|
73
|
+
let newPluginState = currentPluginState;
|
|
23
74
|
if (meta) {
|
|
24
|
-
|
|
75
|
+
if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'SHOW_DIFF') {
|
|
76
|
+
newPluginState = {
|
|
77
|
+
...currentPluginState,
|
|
78
|
+
...meta,
|
|
79
|
+
isDisplayingChanges: true
|
|
80
|
+
};
|
|
81
|
+
} else if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'HIDE_DIFF') {
|
|
82
|
+
newPluginState = {
|
|
83
|
+
...currentPluginState,
|
|
84
|
+
...meta,
|
|
85
|
+
isDisplayingChanges: false
|
|
86
|
+
};
|
|
87
|
+
} else {
|
|
88
|
+
newPluginState = {
|
|
89
|
+
...currentPluginState,
|
|
90
|
+
...meta
|
|
91
|
+
};
|
|
92
|
+
}
|
|
25
93
|
}
|
|
26
|
-
|
|
94
|
+
|
|
95
|
+
// Calculate and store decorations in state
|
|
96
|
+
const decorations = calculateDecorations({
|
|
97
|
+
state: newState,
|
|
98
|
+
pluginState: {
|
|
99
|
+
steps: newPluginState.steps,
|
|
100
|
+
originalDoc: newPluginState.originalDoc
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return {
|
|
104
|
+
...newPluginState,
|
|
105
|
+
decorations
|
|
106
|
+
};
|
|
27
107
|
}
|
|
28
108
|
},
|
|
29
109
|
props: {
|
|
30
110
|
decorations: state => {
|
|
31
111
|
const pluginState = showDiffPluginKey.getState(state);
|
|
32
|
-
|
|
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);
|
|
112
|
+
return pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorations;
|
|
73
113
|
}
|
|
74
114
|
}
|
|
75
115
|
});
|
|
@@ -8,13 +8,17 @@ export const showDiffPlugin = ({
|
|
|
8
8
|
showDiff: params => ({
|
|
9
9
|
tr
|
|
10
10
|
}) => {
|
|
11
|
-
return tr.setMeta(showDiffPluginKey,
|
|
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
|
|
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,6 +12,52 @@ 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
|
+
var calculateDecorations = function calculateDecorations(_ref) {
|
|
16
|
+
var state = _ref.state,
|
|
17
|
+
pluginState = _ref.pluginState;
|
|
18
|
+
var originalDoc = pluginState.originalDoc,
|
|
19
|
+
steps = pluginState.steps;
|
|
20
|
+
if (!originalDoc) {
|
|
21
|
+
return DecorationSet.empty;
|
|
22
|
+
}
|
|
23
|
+
var tr = state.tr;
|
|
24
|
+
var steppedDoc = originalDoc;
|
|
25
|
+
var changeset = ChangeSet.create(originalDoc);
|
|
26
|
+
var _iterator = _createForOfIteratorHelper(steps),
|
|
27
|
+
_step;
|
|
28
|
+
try {
|
|
29
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
30
|
+
var step = _step.value;
|
|
31
|
+
var result = step.apply(steppedDoc);
|
|
32
|
+
if (result.failed === null && result.doc) {
|
|
33
|
+
steppedDoc = result.doc;
|
|
34
|
+
changeset = changeset.addSteps(steppedDoc, [step.getMap()], tr.doc);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
} catch (err) {
|
|
38
|
+
_iterator.e(err);
|
|
39
|
+
} finally {
|
|
40
|
+
_iterator.f();
|
|
41
|
+
}
|
|
42
|
+
if (!steppedDoc.eq(tr.doc)) {
|
|
43
|
+
return DecorationSet.empty;
|
|
44
|
+
}
|
|
45
|
+
var changes = simplifyChanges(changeset.changes, tr.doc);
|
|
46
|
+
var decorations = [];
|
|
47
|
+
changes.forEach(function (change) {
|
|
48
|
+
if (change.inserted.length > 0) {
|
|
49
|
+
decorations.push(createInlineChangedDecoration(change));
|
|
50
|
+
}
|
|
51
|
+
if (change.deleted.length > 0) {
|
|
52
|
+
decorations.push(createDeletedContentDecoration({
|
|
53
|
+
change: change,
|
|
54
|
+
doc: originalDoc,
|
|
55
|
+
tr: tr
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return DecorationSet.empty.add(tr.doc, decorations);
|
|
60
|
+
};
|
|
12
61
|
export var showDiffPluginKey = new PluginKey('showDiffPlugin');
|
|
13
62
|
export var createPlugin = function createPlugin(config) {
|
|
14
63
|
return new SafePlugin({
|
|
@@ -21,66 +70,50 @@ export var createPlugin = function createPlugin(config) {
|
|
|
21
70
|
steps: ((_config$steps = config === null || config === void 0 ? void 0 : config.steps) !== null && _config$steps !== void 0 ? _config$steps : []).map(function (step) {
|
|
22
71
|
return ProseMirrorStep.fromJSON(schema, step);
|
|
23
72
|
}),
|
|
24
|
-
originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined
|
|
73
|
+
originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined,
|
|
74
|
+
decorations: calculateDecorations({
|
|
75
|
+
state: state,
|
|
76
|
+
pluginState: {
|
|
77
|
+
steps: [],
|
|
78
|
+
originalDoc: config !== null && config !== void 0 && config.originalDoc ? processRawValue(state.schema, config.originalDoc) : undefined
|
|
79
|
+
}
|
|
80
|
+
})
|
|
25
81
|
};
|
|
26
82
|
},
|
|
27
|
-
apply: function apply(tr, currentPluginState) {
|
|
83
|
+
apply: function apply(tr, currentPluginState, oldState, newState) {
|
|
28
84
|
var meta = tr.getMeta(showDiffPluginKey);
|
|
85
|
+
var newPluginState = currentPluginState;
|
|
29
86
|
if (meta) {
|
|
30
|
-
|
|
87
|
+
if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'SHOW_DIFF') {
|
|
88
|
+
newPluginState = _objectSpread(_objectSpread(_objectSpread({}, currentPluginState), meta), {}, {
|
|
89
|
+
isDisplayingChanges: true
|
|
90
|
+
});
|
|
91
|
+
} else if ((meta === null || meta === void 0 ? void 0 : meta.action) === 'HIDE_DIFF') {
|
|
92
|
+
newPluginState = _objectSpread(_objectSpread(_objectSpread({}, currentPluginState), meta), {}, {
|
|
93
|
+
isDisplayingChanges: false
|
|
94
|
+
});
|
|
95
|
+
} else {
|
|
96
|
+
newPluginState = _objectSpread(_objectSpread({}, currentPluginState), meta);
|
|
97
|
+
}
|
|
31
98
|
}
|
|
32
|
-
|
|
99
|
+
|
|
100
|
+
// Calculate and store decorations in state
|
|
101
|
+
var decorations = calculateDecorations({
|
|
102
|
+
state: newState,
|
|
103
|
+
pluginState: {
|
|
104
|
+
steps: newPluginState.steps,
|
|
105
|
+
originalDoc: newPluginState.originalDoc
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return _objectSpread(_objectSpread({}, newPluginState), {}, {
|
|
109
|
+
decorations: decorations
|
|
110
|
+
});
|
|
33
111
|
}
|
|
34
112
|
},
|
|
35
113
|
props: {
|
|
36
114
|
decorations: function decorations(state) {
|
|
37
115
|
var pluginState = showDiffPluginKey.getState(state);
|
|
38
|
-
|
|
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);
|
|
116
|
+
return pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorations;
|
|
84
117
|
}
|
|
85
118
|
}
|
|
86
119
|
});
|
|
@@ -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
|
};
|
|
@@ -2,11 +2,13 @@ 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
|
|
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;
|
|
10
12
|
};
|
|
11
13
|
export declare const createPlugin: (config: DiffParams | undefined) => SafePlugin<ShowDiffPluginState>;
|
|
12
14
|
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
|
}>;
|
|
@@ -2,11 +2,13 @@ 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
|
|
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;
|
|
10
12
|
};
|
|
11
13
|
export declare const createPlugin: (config: DiffParams | undefined) => SafePlugin<ShowDiffPluginState>;
|
|
12
14
|
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
|
+
"version": "0.1.0",
|
|
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.
|
|
40
|
+
"@atlaskit/editor-common": "^107.20.0",
|
|
41
41
|
"react": "^18.2.0"
|
|
42
42
|
},
|
|
43
43
|
"techstack": {
|