@atlaskit/editor-plugin-show-diff 5.0.2 → 5.0.3

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,13 @@
1
1
  # @atlaskit/editor-plugin-show-diff
2
2
 
3
+ ## 5.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [`8bddf4c001143`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/8bddf4c001143) -
8
+ Improve scrolling to diffs on the page by focusing before scroll
9
+ - Updated dependencies
10
+
3
11
  ## 5.0.2
4
12
 
5
13
  ### Patch Changes
@@ -14,6 +14,7 @@ var _view = require("@atlaskit/editor-prosemirror/view");
14
14
  var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
15
15
  var _calculateDiffDecorations = require("./calculateDiffDecorations");
16
16
  var _NodeViewSerializer = require("./NodeViewSerializer");
17
+ var _scrollToActiveDecoration = require("./scrollToActiveDecoration");
17
18
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
18
19
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
19
20
  var showDiffPluginKey = exports.showDiffPluginKey = new _state2.PluginKey('showDiffPlugin');
@@ -32,40 +33,6 @@ var createPlugin = exports.createPlugin = function createPlugin(config, getIntl)
32
33
  };
33
34
  return new _safePlugin.SafePlugin({
34
35
  key: showDiffPluginKey,
35
- appendTransaction: function appendTransaction(transactions, oldState, newState) {
36
- var _pluginState$activeIn;
37
- if (!(0, _platformFeatureFlags.fg)('platform_editor_show_diff_scroll_navigation')) {
38
- return;
39
- }
40
- // Check if any transaction contains scroll actions
41
- var scrollTransaction = transactions.find(function (tr) {
42
- var _tr$getMeta, _tr$getMeta2;
43
- return ((_tr$getMeta = tr.getMeta(showDiffPluginKey)) === null || _tr$getMeta === void 0 ? void 0 : _tr$getMeta.action) === 'SCROLL_TO_NEXT' || ((_tr$getMeta2 = tr.getMeta(showDiffPluginKey)) === null || _tr$getMeta2 === void 0 ? void 0 : _tr$getMeta2.action) === 'SCROLL_TO_PREVIOUS';
44
- });
45
- if (!scrollTransaction) {
46
- return;
47
- }
48
- var pluginState = showDiffPluginKey.getState(newState);
49
- if (!pluginState || pluginState.decorations.find().length === 0) {
50
- return;
51
- }
52
- var decorations = getScrollableDecorations(pluginState.decorations);
53
- var decoration = decorations[(_pluginState$activeIn = pluginState.activeIndex) !== null && _pluginState$activeIn !== void 0 ? _pluginState$activeIn : 0];
54
- if (!decoration) {
55
- return;
56
- }
57
- var from = decoration.from,
58
- to = decoration.to;
59
- var $pos = newState.doc.resolve(from);
60
- var isNodeSelection = $pos.nodeAfter && $pos.nodeAfter.nodeSize === to - from && !$pos.nodeAfter.isInline;
61
- var tr = newState.tr;
62
- if (isNodeSelection) {
63
- tr.setSelection(_state2.NodeSelection.create(newState.doc, from));
64
- } else {
65
- tr.setSelection(_state2.TextSelection.create(newState.doc, from));
66
- }
67
- return tr.scrollIntoView();
68
- },
69
36
  state: {
70
37
  init: function init(_, _state) {
71
38
  // We do initial setup after we setup the editor view
@@ -145,6 +112,7 @@ var createPlugin = exports.createPlugin = function createPlugin(config, getIntl)
145
112
  view: function view(editorView) {
146
113
  setNodeViewSerializer(editorView);
147
114
  var isFirst = true;
115
+ var previousActiveIndex;
148
116
  return {
149
117
  update: function update(view) {
150
118
  // If we're using configuration to show diffs we initialise here once we setup the editor view
@@ -158,6 +126,16 @@ var createPlugin = exports.createPlugin = function createPlugin(config, getIntl)
158
126
  originalDoc: (0, _processRawValue.processRawValue)(view.state.schema, config.originalDoc)
159
127
  }));
160
128
  }
129
+
130
+ // Check for any potential scroll side-effects
131
+ if ((0, _platformFeatureFlags.fg)('platform_editor_show_diff_scroll_navigation')) {
132
+ var pluginState = showDiffPluginKey.getState(view.state);
133
+ var activeIndexChanged = (pluginState === null || pluginState === void 0 ? void 0 : pluginState.activeIndex) !== undefined && pluginState.activeIndex !== previousActiveIndex;
134
+ previousActiveIndex = pluginState === null || pluginState === void 0 ? void 0 : pluginState.activeIndex;
135
+ if (pluginState !== null && pluginState !== void 0 && pluginState.activeIndex && activeIndexChanged) {
136
+ (0, _scrollToActiveDecoration.scrollToActiveDecoration)(view, getScrollableDecorations(pluginState.decorations), pluginState.activeIndex);
137
+ }
138
+ }
161
139
  }
162
140
  };
163
141
  },
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.scrollToActiveDecoration = void 0;
7
+ /**
8
+ * Scrolls to the current position/selection of the document. It does the same as scrollIntoView()
9
+ * but without requiring the focus on the editor, thus it can be called at any time.
10
+ */
11
+ function scrollToSelection(view, position) {
12
+ var _view$domAtPos = view.domAtPos(position),
13
+ node = _view$domAtPos.node;
14
+ if (node instanceof Element) {
15
+ node.scrollIntoView({
16
+ behavior: 'smooth',
17
+ block: 'center'
18
+ });
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Scrolls the editor view to the decoration at the given index.
24
+ */
25
+ var scrollToActiveDecoration = exports.scrollToActiveDecoration = function scrollToActiveDecoration(view, decorations, activeIndex) {
26
+ var decoration = decorations[activeIndex];
27
+ if (!decoration) {
28
+ return;
29
+ }
30
+ scrollToSelection(view, decoration === null || decoration === void 0 ? void 0 : decoration.from);
31
+ };
@@ -1,11 +1,12 @@
1
1
  import { processRawValue } from '@atlaskit/editor-common/process-raw-value';
2
2
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
3
- import { PluginKey, NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
3
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
4
4
  import { Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform';
5
5
  import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
6
6
  import { fg } from '@atlaskit/platform-feature-flags';
7
7
  import { calculateDiffDecorations } from './calculateDiffDecorations';
8
8
  import { NodeViewSerializer } from './NodeViewSerializer';
9
+ import { scrollToActiveDecoration } from './scrollToActiveDecoration';
9
10
  export const showDiffPluginKey = new PluginKey('showDiffPlugin');
10
11
  export const getScrollableDecorations = set => {
11
12
  var _set$find;
@@ -20,42 +21,6 @@ export const createPlugin = (config, getIntl) => {
20
21
  };
21
22
  return new SafePlugin({
22
23
  key: showDiffPluginKey,
23
- appendTransaction(transactions, oldState, newState) {
24
- var _pluginState$activeIn;
25
- if (!fg('platform_editor_show_diff_scroll_navigation')) {
26
- return;
27
- }
28
- // Check if any transaction contains scroll actions
29
- const scrollTransaction = transactions.find(tr => {
30
- var _tr$getMeta, _tr$getMeta2;
31
- return ((_tr$getMeta = tr.getMeta(showDiffPluginKey)) === null || _tr$getMeta === void 0 ? void 0 : _tr$getMeta.action) === 'SCROLL_TO_NEXT' || ((_tr$getMeta2 = tr.getMeta(showDiffPluginKey)) === null || _tr$getMeta2 === void 0 ? void 0 : _tr$getMeta2.action) === 'SCROLL_TO_PREVIOUS';
32
- });
33
- if (!scrollTransaction) {
34
- return;
35
- }
36
- const pluginState = showDiffPluginKey.getState(newState);
37
- if (!pluginState || pluginState.decorations.find().length === 0) {
38
- return;
39
- }
40
- const decorations = getScrollableDecorations(pluginState.decorations);
41
- const decoration = decorations[(_pluginState$activeIn = pluginState.activeIndex) !== null && _pluginState$activeIn !== void 0 ? _pluginState$activeIn : 0];
42
- if (!decoration) {
43
- return;
44
- }
45
- const {
46
- from,
47
- to
48
- } = decoration;
49
- const $pos = newState.doc.resolve(from);
50
- const isNodeSelection = $pos.nodeAfter && $pos.nodeAfter.nodeSize === to - from && !$pos.nodeAfter.isInline;
51
- const tr = newState.tr;
52
- if (isNodeSelection) {
53
- tr.setSelection(NodeSelection.create(newState.doc, from));
54
- } else {
55
- tr.setSelection(TextSelection.create(newState.doc, from));
56
- }
57
- return tr.scrollIntoView();
58
- },
59
24
  state: {
60
25
  init(_, _state) {
61
26
  // We do initial setup after we setup the editor view
@@ -144,6 +109,7 @@ export const createPlugin = (config, getIntl) => {
144
109
  view(editorView) {
145
110
  setNodeViewSerializer(editorView);
146
111
  let isFirst = true;
112
+ let previousActiveIndex;
147
113
  return {
148
114
  update(view) {
149
115
  // If we're using configuration to show diffs we initialise here once we setup the editor view
@@ -155,6 +121,16 @@ export const createPlugin = (config, getIntl) => {
155
121
  originalDoc: processRawValue(view.state.schema, config.originalDoc)
156
122
  }));
157
123
  }
124
+
125
+ // Check for any potential scroll side-effects
126
+ if (fg('platform_editor_show_diff_scroll_navigation')) {
127
+ const pluginState = showDiffPluginKey.getState(view.state);
128
+ const activeIndexChanged = (pluginState === null || pluginState === void 0 ? void 0 : pluginState.activeIndex) !== undefined && pluginState.activeIndex !== previousActiveIndex;
129
+ previousActiveIndex = pluginState === null || pluginState === void 0 ? void 0 : pluginState.activeIndex;
130
+ if (pluginState !== null && pluginState !== void 0 && pluginState.activeIndex && activeIndexChanged) {
131
+ scrollToActiveDecoration(view, getScrollableDecorations(pluginState.decorations), pluginState.activeIndex);
132
+ }
133
+ }
158
134
  }
159
135
  };
160
136
  },
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Scrolls to the current position/selection of the document. It does the same as scrollIntoView()
3
+ * but without requiring the focus on the editor, thus it can be called at any time.
4
+ */
5
+ function scrollToSelection(view, position) {
6
+ const {
7
+ node
8
+ } = view.domAtPos(position);
9
+ if (node instanceof Element) {
10
+ node.scrollIntoView({
11
+ behavior: 'smooth',
12
+ block: 'center'
13
+ });
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Scrolls the editor view to the decoration at the given index.
19
+ */
20
+ export const scrollToActiveDecoration = (view, decorations, activeIndex) => {
21
+ const decoration = decorations[activeIndex];
22
+ if (!decoration) {
23
+ return;
24
+ }
25
+ scrollToSelection(view, decoration === null || decoration === void 0 ? void 0 : decoration.from);
26
+ };
@@ -3,12 +3,13 @@ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbol
3
3
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
4
  import { processRawValue } from '@atlaskit/editor-common/process-raw-value';
5
5
  import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
6
- import { PluginKey, NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state';
6
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
7
7
  import { Step as ProseMirrorStep } from '@atlaskit/editor-prosemirror/transform';
8
8
  import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
9
9
  import { fg } from '@atlaskit/platform-feature-flags';
10
10
  import { calculateDiffDecorations } from './calculateDiffDecorations';
11
11
  import { NodeViewSerializer } from './NodeViewSerializer';
12
+ import { scrollToActiveDecoration } from './scrollToActiveDecoration';
12
13
  export var showDiffPluginKey = new PluginKey('showDiffPlugin');
13
14
  export var getScrollableDecorations = function getScrollableDecorations(set) {
14
15
  var _set$find;
@@ -25,40 +26,6 @@ export var createPlugin = function createPlugin(config, getIntl) {
25
26
  };
26
27
  return new SafePlugin({
27
28
  key: showDiffPluginKey,
28
- appendTransaction: function appendTransaction(transactions, oldState, newState) {
29
- var _pluginState$activeIn;
30
- if (!fg('platform_editor_show_diff_scroll_navigation')) {
31
- return;
32
- }
33
- // Check if any transaction contains scroll actions
34
- var scrollTransaction = transactions.find(function (tr) {
35
- var _tr$getMeta, _tr$getMeta2;
36
- return ((_tr$getMeta = tr.getMeta(showDiffPluginKey)) === null || _tr$getMeta === void 0 ? void 0 : _tr$getMeta.action) === 'SCROLL_TO_NEXT' || ((_tr$getMeta2 = tr.getMeta(showDiffPluginKey)) === null || _tr$getMeta2 === void 0 ? void 0 : _tr$getMeta2.action) === 'SCROLL_TO_PREVIOUS';
37
- });
38
- if (!scrollTransaction) {
39
- return;
40
- }
41
- var pluginState = showDiffPluginKey.getState(newState);
42
- if (!pluginState || pluginState.decorations.find().length === 0) {
43
- return;
44
- }
45
- var decorations = getScrollableDecorations(pluginState.decorations);
46
- var decoration = decorations[(_pluginState$activeIn = pluginState.activeIndex) !== null && _pluginState$activeIn !== void 0 ? _pluginState$activeIn : 0];
47
- if (!decoration) {
48
- return;
49
- }
50
- var from = decoration.from,
51
- to = decoration.to;
52
- var $pos = newState.doc.resolve(from);
53
- var isNodeSelection = $pos.nodeAfter && $pos.nodeAfter.nodeSize === to - from && !$pos.nodeAfter.isInline;
54
- var tr = newState.tr;
55
- if (isNodeSelection) {
56
- tr.setSelection(NodeSelection.create(newState.doc, from));
57
- } else {
58
- tr.setSelection(TextSelection.create(newState.doc, from));
59
- }
60
- return tr.scrollIntoView();
61
- },
62
29
  state: {
63
30
  init: function init(_, _state) {
64
31
  // We do initial setup after we setup the editor view
@@ -138,6 +105,7 @@ export var createPlugin = function createPlugin(config, getIntl) {
138
105
  view: function view(editorView) {
139
106
  setNodeViewSerializer(editorView);
140
107
  var isFirst = true;
108
+ var previousActiveIndex;
141
109
  return {
142
110
  update: function update(view) {
143
111
  // If we're using configuration to show diffs we initialise here once we setup the editor view
@@ -151,6 +119,16 @@ export var createPlugin = function createPlugin(config, getIntl) {
151
119
  originalDoc: processRawValue(view.state.schema, config.originalDoc)
152
120
  }));
153
121
  }
122
+
123
+ // Check for any potential scroll side-effects
124
+ if (fg('platform_editor_show_diff_scroll_navigation')) {
125
+ var pluginState = showDiffPluginKey.getState(view.state);
126
+ var activeIndexChanged = (pluginState === null || pluginState === void 0 ? void 0 : pluginState.activeIndex) !== undefined && pluginState.activeIndex !== previousActiveIndex;
127
+ previousActiveIndex = pluginState === null || pluginState === void 0 ? void 0 : pluginState.activeIndex;
128
+ if (pluginState !== null && pluginState !== void 0 && pluginState.activeIndex && activeIndexChanged) {
129
+ scrollToActiveDecoration(view, getScrollableDecorations(pluginState.decorations), pluginState.activeIndex);
130
+ }
131
+ }
154
132
  }
155
133
  };
156
134
  },
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Scrolls to the current position/selection of the document. It does the same as scrollIntoView()
3
+ * but without requiring the focus on the editor, thus it can be called at any time.
4
+ */
5
+ function scrollToSelection(view, position) {
6
+ var _view$domAtPos = view.domAtPos(position),
7
+ node = _view$domAtPos.node;
8
+ if (node instanceof Element) {
9
+ node.scrollIntoView({
10
+ behavior: 'smooth',
11
+ block: 'center'
12
+ });
13
+ }
14
+ }
15
+
16
+ /**
17
+ * Scrolls the editor view to the decoration at the given index.
18
+ */
19
+ export var scrollToActiveDecoration = function scrollToActiveDecoration(view, decorations, activeIndex) {
20
+ var decoration = decorations[activeIndex];
21
+ if (!decoration) {
22
+ return;
23
+ }
24
+ scrollToSelection(view, decoration === null || decoration === void 0 ? void 0 : decoration.from);
25
+ };
@@ -0,0 +1,5 @@
1
+ import type { EditorView, Decoration } from '@atlaskit/editor-prosemirror/view';
2
+ /**
3
+ * Scrolls the editor view to the decoration at the given index.
4
+ */
5
+ export declare const scrollToActiveDecoration: (view: EditorView, decorations: Decoration[], activeIndex: number) => void;
@@ -0,0 +1,5 @@
1
+ import type { EditorView, Decoration } from '@atlaskit/editor-prosemirror/view';
2
+ /**
3
+ * Scrolls the editor view to the decoration at the given index.
4
+ */
5
+ export declare const scrollToActiveDecoration: (view: EditorView, decorations: Decoration[], activeIndex: number) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-show-diff",
3
- "version": "5.0.2",
3
+ "version": "5.0.3",
4
4
  "description": "ShowDiff plugin for @atlaskit/editor-core",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -32,7 +32,7 @@
32
32
  "@atlaskit/editor-prosemirror": "^7.3.0",
33
33
  "@atlaskit/editor-tables": "^2.9.0",
34
34
  "@atlaskit/platform-feature-flags": "^1.1.0",
35
- "@atlaskit/tmp-editor-statsig": "^34.0.0",
35
+ "@atlaskit/tmp-editor-statsig": "^34.4.0",
36
36
  "@atlaskit/tokens": "^11.0.0",
37
37
  "@babel/runtime": "^7.0.0",
38
38
  "lodash": "^4.17.21",
@@ -40,7 +40,7 @@
40
40
  "prosemirror-changeset": "^2.3.1"
41
41
  },
42
42
  "peerDependencies": {
43
- "@atlaskit/editor-common": "^111.28.0",
43
+ "@atlaskit/editor-common": "^111.29.0",
44
44
  "react": "^18.2.0"
45
45
  },
46
46
  "techstack": {