@atlaskit/editor-plugin-code-block 3.3.2 → 3.3.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,12 @@
1
1
  # @atlaskit/editor-plugin-code-block
2
2
 
3
+ ## 3.3.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [`0f58d3a82867c`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/0f58d3a82867c) -
8
+ ED-24500 Add decorators on init of code block and on document updates
9
+
3
10
  ## 3.3.2
4
11
 
5
12
  ### Patch Changes
@@ -4,17 +4,25 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.generateLineAttributesFromNode = exports.createLineNumbersDecorations = exports.createDecorationSetFromLineAttributes = void 0;
7
+ exports.generateLineAttributesFromNode = exports.createLineNumbersDecorations = exports.createDecorationSetFromLineAttributes = exports.DECORATION_WIDGET_TYPE = void 0;
8
8
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
9
  var _view = require("@atlaskit/editor-prosemirror/view");
10
10
  var _classNames = require("../ui/class-names");
11
- var DECORATION_WIDGET_TYPE = 'decorationWidgetType';
11
+ var DECORATION_WIDGET_TYPE = exports.DECORATION_WIDGET_TYPE = 'decorationWidgetType';
12
12
  var generateLineAttributesFromNode = exports.generateLineAttributesFromNode = function generateLineAttributesFromNode(pos, node) {
13
13
  // Get content node
14
14
  var contentNode = node.content;
15
15
 
16
16
  // Get node text content
17
17
  var lineAttributes = [];
18
+
19
+ // Early exit if content size is 0
20
+ if (contentNode.size === 0) {
21
+ return [{
22
+ lineStart: pos + 1,
23
+ lineNumber: 1
24
+ }];
25
+ }
18
26
  contentNode.forEach(function (child) {
19
27
  var nodeTextContent = child.textContent;
20
28
  var nodeStartPos = pos;
@@ -32,6 +32,17 @@ var createPlugin = exports.createPlugin = function createPlugin(_ref) {
32
32
  allowCompositionInputOverride = _ref$allowComposition === void 0 ? false : _ref$allowComposition,
33
33
  api = _ref.api;
34
34
  var handleDOMEvents = {};
35
+ var createLineNumberDecoratorsFromDecendants = function createLineNumberDecoratorsFromDecendants(editorState) {
36
+ var lineNumberDecorators = [];
37
+ editorState.doc.descendants(function (node, pos) {
38
+ if (node.type === editorState.schema.nodes.codeBlock) {
39
+ lineNumberDecorators.push.apply(lineNumberDecorators, (0, _toConsumableArray2.default)((0, _decorators.createLineNumbersDecorations)(pos, node)));
40
+ return false;
41
+ }
42
+ return true;
43
+ });
44
+ return lineNumberDecorators;
45
+ };
35
46
  var createWordWrappedDecoratorPluginState = function createWordWrappedDecoratorPluginState(pluginState, tr, node) {
36
47
  var decorationSetFromState = pluginState.decorations;
37
48
  if (node) {
@@ -59,19 +70,19 @@ var createPlugin = exports.createPlugin = function createPlugin(_ref) {
59
70
  }
60
71
  return decorationSetFromState;
61
72
  };
62
- var createLineDecoratorPluginState = function createLineDecoratorPluginState(pluginState, tr, node) {
73
+ var createLineDecoratorPluginState = function createLineDecoratorPluginState(pluginState, tr, state) {
63
74
  var decorationSetFromState = pluginState.decorations;
64
- if (node) {
65
- var pos = node.pos,
66
- innerNode = node.node;
67
- if (pos !== undefined) {
68
- // Reset the decorations for the children of the code block node. Wipes all line number decorations.
69
- // @ts-ignore
70
- decorationSetFromState.children = [];
71
- var lineDecorators = (0, _decorators.createLineNumbersDecorations)(pos, innerNode);
72
- decorationSetFromState = decorationSetFromState.add(tr.doc, (0, _toConsumableArray2.default)(lineDecorators));
73
- }
74
- }
75
+ // remove all the line number children from the decorations set. 'undefined, undefined' is used to find() the whole doc.
76
+ var children = decorationSetFromState.find(undefined, undefined, function (spec) {
77
+ return spec.type === _decorators.DECORATION_WIDGET_TYPE;
78
+ });
79
+ decorationSetFromState = decorationSetFromState.remove(children);
80
+
81
+ // regenerate all the line number for the documents code blocks
82
+ var lineDecorators = createLineNumberDecoratorsFromDecendants(state);
83
+
84
+ // add the newly generated line numbers to the decorations set
85
+ decorationSetFromState = decorationSetFromState.add(tr.doc, (0, _toConsumableArray2.default)(lineDecorators));
75
86
  return decorationSetFromState;
76
87
  };
77
88
 
@@ -117,12 +128,13 @@ var createPlugin = exports.createPlugin = function createPlugin(_ref) {
117
128
  state: {
118
129
  init: function init(_, state) {
119
130
  var node = (0, _utils2.findCodeBlock)(state, state.selection);
131
+ var lineNumberDecorators = (0, _platformFeatureFlags.fg)('editor_support_code_block_wrapping') ? createLineNumberDecoratorsFromDecendants(state) : [];
120
132
  return {
121
133
  pos: node ? node.pos : null,
122
134
  contentCopied: false,
123
135
  isNodeSelected: false,
124
136
  shouldIgnoreFollowingMutations: false,
125
- decorations: _view.DecorationSet.empty
137
+ decorations: _view.DecorationSet.create(state.doc, lineNumberDecorators)
126
138
  };
127
139
  },
128
140
  apply: function apply(tr, pluginState, _oldState, newState) {
@@ -138,7 +150,7 @@ var createPlugin = exports.createPlugin = function createPlugin(_ref) {
138
150
  var newPluginState = _objectSpread(_objectSpread({}, pluginState), {}, {
139
151
  pos: _node ? _node.pos : null,
140
152
  isNodeSelected: tr.selection instanceof _state.NodeSelection,
141
- decorations: (0, _platformFeatureFlags.fg)('editor_support_code_block_wrapping') ? createLineDecoratorPluginState(pluginState, tr, _node) : _view.DecorationSet.empty
153
+ decorations: (0, _platformFeatureFlags.fg)('editor_support_code_block_wrapping') ? createLineDecoratorPluginState(pluginState, tr, newState) : _view.DecorationSet.empty
142
154
  });
143
155
  return newPluginState;
144
156
  }
@@ -1,12 +1,20 @@
1
1
  import { Decoration } from '@atlaskit/editor-prosemirror/view';
2
2
  import { codeBlockClassNames } from '../ui/class-names';
3
- const DECORATION_WIDGET_TYPE = 'decorationWidgetType';
3
+ export const DECORATION_WIDGET_TYPE = 'decorationWidgetType';
4
4
  export const generateLineAttributesFromNode = (pos, node) => {
5
5
  // Get content node
6
6
  const contentNode = node.content;
7
7
 
8
8
  // Get node text content
9
9
  let lineAttributes = [];
10
+
11
+ // Early exit if content size is 0
12
+ if (contentNode.size === 0) {
13
+ return [{
14
+ lineStart: pos + 1,
15
+ lineNumber: 1
16
+ }];
17
+ }
10
18
  contentNode.forEach(child => {
11
19
  const nodeTextContent = child.textContent;
12
20
  const nodeStartPos = pos;
@@ -11,7 +11,7 @@ import { pluginKey } from '../plugin-key';
11
11
  import { codeBlockClassNames } from '../ui/class-names';
12
12
  import { findCodeBlock } from '../utils';
13
13
  import { ACTIONS } from './actions';
14
- import { createLineNumbersDecorations } from './decorators';
14
+ import { createLineNumbersDecorations, DECORATION_WIDGET_TYPE } from './decorators';
15
15
  const DECORATION_WRAPPED_BLOCK_NODE_TYPE = 'decorationNodeType';
16
16
  export const createPlugin = ({
17
17
  useLongPressSelection = false,
@@ -20,6 +20,17 @@ export const createPlugin = ({
20
20
  api
21
21
  }) => {
22
22
  const handleDOMEvents = {};
23
+ const createLineNumberDecoratorsFromDecendants = editorState => {
24
+ let lineNumberDecorators = [];
25
+ editorState.doc.descendants((node, pos) => {
26
+ if (node.type === editorState.schema.nodes.codeBlock) {
27
+ lineNumberDecorators.push(...createLineNumbersDecorations(pos, node));
28
+ return false;
29
+ }
30
+ return true;
31
+ });
32
+ return lineNumberDecorators;
33
+ };
23
34
  const createWordWrappedDecoratorPluginState = (pluginState, tr, node) => {
24
35
  let decorationSetFromState = pluginState.decorations;
25
36
  if (node) {
@@ -47,21 +58,17 @@ export const createPlugin = ({
47
58
  }
48
59
  return decorationSetFromState;
49
60
  };
50
- const createLineDecoratorPluginState = (pluginState, tr, node) => {
61
+ const createLineDecoratorPluginState = (pluginState, tr, state) => {
51
62
  let decorationSetFromState = pluginState.decorations;
52
- if (node) {
53
- const {
54
- pos,
55
- node: innerNode
56
- } = node;
57
- if (pos !== undefined) {
58
- // Reset the decorations for the children of the code block node. Wipes all line number decorations.
59
- // @ts-ignore
60
- decorationSetFromState.children = [];
61
- const lineDecorators = createLineNumbersDecorations(pos, innerNode);
62
- decorationSetFromState = decorationSetFromState.add(tr.doc, [...lineDecorators]);
63
- }
64
- }
63
+ // remove all the line number children from the decorations set. 'undefined, undefined' is used to find() the whole doc.
64
+ const children = decorationSetFromState.find(undefined, undefined, spec => spec.type === DECORATION_WIDGET_TYPE);
65
+ decorationSetFromState = decorationSetFromState.remove(children);
66
+
67
+ // regenerate all the line number for the documents code blocks
68
+ const lineDecorators = createLineNumberDecoratorsFromDecendants(state);
69
+
70
+ // add the newly generated line numbers to the decorations set
71
+ decorationSetFromState = decorationSetFromState.add(tr.doc, [...lineDecorators]);
65
72
  return decorationSetFromState;
66
73
  };
67
74
 
@@ -105,12 +112,13 @@ export const createPlugin = ({
105
112
  state: {
106
113
  init(_, state) {
107
114
  const node = findCodeBlock(state, state.selection);
115
+ const lineNumberDecorators = fg('editor_support_code_block_wrapping') ? createLineNumberDecoratorsFromDecendants(state) : [];
108
116
  return {
109
117
  pos: node ? node.pos : null,
110
118
  contentCopied: false,
111
119
  isNodeSelected: false,
112
120
  shouldIgnoreFollowingMutations: false,
113
- decorations: DecorationSet.empty
121
+ decorations: DecorationSet.create(state.doc, lineNumberDecorators)
114
122
  };
115
123
  },
116
124
  apply(tr, pluginState, _oldState, newState) {
@@ -128,7 +136,7 @@ export const createPlugin = ({
128
136
  ...pluginState,
129
137
  pos: node ? node.pos : null,
130
138
  isNodeSelected: tr.selection instanceof NodeSelection,
131
- decorations: fg('editor_support_code_block_wrapping') ? createLineDecoratorPluginState(pluginState, tr, node) : DecorationSet.empty
139
+ decorations: fg('editor_support_code_block_wrapping') ? createLineDecoratorPluginState(pluginState, tr, newState) : DecorationSet.empty
132
140
  };
133
141
  return newPluginState;
134
142
  }
@@ -1,13 +1,21 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
2
  import { Decoration } from '@atlaskit/editor-prosemirror/view';
3
3
  import { codeBlockClassNames } from '../ui/class-names';
4
- var DECORATION_WIDGET_TYPE = 'decorationWidgetType';
4
+ export var DECORATION_WIDGET_TYPE = 'decorationWidgetType';
5
5
  export var generateLineAttributesFromNode = function generateLineAttributesFromNode(pos, node) {
6
6
  // Get content node
7
7
  var contentNode = node.content;
8
8
 
9
9
  // Get node text content
10
10
  var lineAttributes = [];
11
+
12
+ // Early exit if content size is 0
13
+ if (contentNode.size === 0) {
14
+ return [{
15
+ lineStart: pos + 1,
16
+ lineNumber: 1
17
+ }];
18
+ }
11
19
  contentNode.forEach(function (child) {
12
20
  var nodeTextContent = child.textContent;
13
21
  var nodeStartPos = pos;
@@ -15,7 +15,7 @@ import { pluginKey } from '../plugin-key';
15
15
  import { codeBlockClassNames } from '../ui/class-names';
16
16
  import { findCodeBlock } from '../utils';
17
17
  import { ACTIONS } from './actions';
18
- import { createLineNumbersDecorations } from './decorators';
18
+ import { createLineNumbersDecorations, DECORATION_WIDGET_TYPE } from './decorators';
19
19
  var DECORATION_WRAPPED_BLOCK_NODE_TYPE = 'decorationNodeType';
20
20
  export var createPlugin = function createPlugin(_ref) {
21
21
  var _ref$useLongPressSele = _ref.useLongPressSelection,
@@ -25,6 +25,17 @@ export var createPlugin = function createPlugin(_ref) {
25
25
  allowCompositionInputOverride = _ref$allowComposition === void 0 ? false : _ref$allowComposition,
26
26
  api = _ref.api;
27
27
  var handleDOMEvents = {};
28
+ var createLineNumberDecoratorsFromDecendants = function createLineNumberDecoratorsFromDecendants(editorState) {
29
+ var lineNumberDecorators = [];
30
+ editorState.doc.descendants(function (node, pos) {
31
+ if (node.type === editorState.schema.nodes.codeBlock) {
32
+ lineNumberDecorators.push.apply(lineNumberDecorators, _toConsumableArray(createLineNumbersDecorations(pos, node)));
33
+ return false;
34
+ }
35
+ return true;
36
+ });
37
+ return lineNumberDecorators;
38
+ };
28
39
  var createWordWrappedDecoratorPluginState = function createWordWrappedDecoratorPluginState(pluginState, tr, node) {
29
40
  var decorationSetFromState = pluginState.decorations;
30
41
  if (node) {
@@ -52,19 +63,19 @@ export var createPlugin = function createPlugin(_ref) {
52
63
  }
53
64
  return decorationSetFromState;
54
65
  };
55
- var createLineDecoratorPluginState = function createLineDecoratorPluginState(pluginState, tr, node) {
66
+ var createLineDecoratorPluginState = function createLineDecoratorPluginState(pluginState, tr, state) {
56
67
  var decorationSetFromState = pluginState.decorations;
57
- if (node) {
58
- var pos = node.pos,
59
- innerNode = node.node;
60
- if (pos !== undefined) {
61
- // Reset the decorations for the children of the code block node. Wipes all line number decorations.
62
- // @ts-ignore
63
- decorationSetFromState.children = [];
64
- var lineDecorators = createLineNumbersDecorations(pos, innerNode);
65
- decorationSetFromState = decorationSetFromState.add(tr.doc, _toConsumableArray(lineDecorators));
66
- }
67
- }
68
+ // remove all the line number children from the decorations set. 'undefined, undefined' is used to find() the whole doc.
69
+ var children = decorationSetFromState.find(undefined, undefined, function (spec) {
70
+ return spec.type === DECORATION_WIDGET_TYPE;
71
+ });
72
+ decorationSetFromState = decorationSetFromState.remove(children);
73
+
74
+ // regenerate all the line number for the documents code blocks
75
+ var lineDecorators = createLineNumberDecoratorsFromDecendants(state);
76
+
77
+ // add the newly generated line numbers to the decorations set
78
+ decorationSetFromState = decorationSetFromState.add(tr.doc, _toConsumableArray(lineDecorators));
68
79
  return decorationSetFromState;
69
80
  };
70
81
 
@@ -110,12 +121,13 @@ export var createPlugin = function createPlugin(_ref) {
110
121
  state: {
111
122
  init: function init(_, state) {
112
123
  var node = findCodeBlock(state, state.selection);
124
+ var lineNumberDecorators = fg('editor_support_code_block_wrapping') ? createLineNumberDecoratorsFromDecendants(state) : [];
113
125
  return {
114
126
  pos: node ? node.pos : null,
115
127
  contentCopied: false,
116
128
  isNodeSelected: false,
117
129
  shouldIgnoreFollowingMutations: false,
118
- decorations: DecorationSet.empty
130
+ decorations: DecorationSet.create(state.doc, lineNumberDecorators)
119
131
  };
120
132
  },
121
133
  apply: function apply(tr, pluginState, _oldState, newState) {
@@ -131,7 +143,7 @@ export var createPlugin = function createPlugin(_ref) {
131
143
  var newPluginState = _objectSpread(_objectSpread({}, pluginState), {}, {
132
144
  pos: _node ? _node.pos : null,
133
145
  isNodeSelected: tr.selection instanceof NodeSelection,
134
- decorations: fg('editor_support_code_block_wrapping') ? createLineDecoratorPluginState(pluginState, tr, _node) : DecorationSet.empty
146
+ decorations: fg('editor_support_code_block_wrapping') ? createLineDecoratorPluginState(pluginState, tr, newState) : DecorationSet.empty
135
147
  });
136
148
  return newPluginState;
137
149
  }
@@ -1,6 +1,7 @@
1
1
  import type { Node } from '@atlaskit/editor-prosemirror/model';
2
2
  import { Decoration } from '@atlaskit/editor-prosemirror/view';
3
3
  import type { CodeBlockLineAttributes } from '../types';
4
+ export declare const DECORATION_WIDGET_TYPE = "decorationWidgetType";
4
5
  export declare const generateLineAttributesFromNode: (pos: number, node: Node) => CodeBlockLineAttributes[];
5
6
  export declare const createDecorationSetFromLineAttributes: (lineAttributes: CodeBlockLineAttributes[]) => Decoration[];
6
7
  export declare const createLineNumbersDecorations: (pos: number, node: Node) => Decoration[];
@@ -1,6 +1,7 @@
1
1
  import type { Node } from '@atlaskit/editor-prosemirror/model';
2
2
  import { Decoration } from '@atlaskit/editor-prosemirror/view';
3
3
  import type { CodeBlockLineAttributes } from '../types';
4
+ export declare const DECORATION_WIDGET_TYPE = "decorationWidgetType";
4
5
  export declare const generateLineAttributesFromNode: (pos: number, node: Node) => CodeBlockLineAttributes[];
5
6
  export declare const createDecorationSetFromLineAttributes: (lineAttributes: CodeBlockLineAttributes[]) => Decoration[];
6
7
  export declare const createLineNumbersDecorations: (pos: number, node: Node) => Decoration[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-code-block",
3
- "version": "3.3.2",
3
+ "version": "3.3.3",
4
4
  "description": "Code block plugin for @atlaskit/editor-core",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -35,13 +35,13 @@
35
35
  "dependencies": {
36
36
  "@atlaskit/adf-schema": "^40.8.1",
37
37
  "@atlaskit/code": "^15.5.0",
38
- "@atlaskit/editor-common": "^87.8.0",
38
+ "@atlaskit/editor-common": "^87.10.0",
39
39
  "@atlaskit/editor-plugin-analytics": "^1.7.0",
40
40
  "@atlaskit/editor-plugin-composition": "^1.2.0",
41
41
  "@atlaskit/editor-plugin-decorations": "^1.2.0",
42
42
  "@atlaskit/editor-plugin-editor-disabled": "^1.2.0",
43
43
  "@atlaskit/editor-prosemirror": "5.0.1",
44
- "@atlaskit/icon": "^22.12.0",
44
+ "@atlaskit/icon": "^22.13.0",
45
45
  "@atlaskit/platform-feature-flags": "^0.3.0",
46
46
  "@atlaskit/prosemirror-input-rules": "^3.2.0",
47
47
  "@babel/runtime": "^7.0.0",