@atlaskit/editor-plugin-code-block 3.3.2 → 3.3.4
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 +17 -0
- package/dist/cjs/nodeviews/code-block.js +20 -2
- package/dist/cjs/pm-plugins/decorators.js +10 -2
- package/dist/cjs/pm-plugins/main.js +27 -15
- package/dist/es2019/nodeviews/code-block.js +20 -2
- package/dist/es2019/pm-plugins/decorators.js +9 -1
- package/dist/es2019/pm-plugins/main.js +26 -18
- package/dist/esm/nodeviews/code-block.js +20 -2
- package/dist/esm/pm-plugins/decorators.js +9 -1
- package/dist/esm/pm-plugins/main.js +28 -16
- package/dist/types/nodeviews/code-block.d.ts +4 -0
- package/dist/types/pm-plugins/decorators.d.ts +1 -0
- package/dist/types-ts4.5/nodeviews/code-block.d.ts +4 -0
- package/dist/types-ts4.5/pm-plugins/decorators.d.ts +1 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @atlaskit/editor-plugin-code-block
|
|
2
2
|
|
|
3
|
+
## 3.3.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#128388](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/128388)
|
|
8
|
+
[`0ea0f81d30dcc`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/0ea0f81d30dcc) -
|
|
9
|
+
ED-24501 Add a CSS var to maintain a dynamic line number gutter size on the code block based on
|
|
10
|
+
the max number length. ED-24573 update handleClickOn in the code block plugin to work with new
|
|
11
|
+
line number gutter.
|
|
12
|
+
|
|
13
|
+
## 3.3.3
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- [`0f58d3a82867c`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/0f58d3a82867c) -
|
|
18
|
+
ED-24500 Add decorators on init of code block and on document updates
|
|
19
|
+
|
|
3
20
|
## 3.3.2
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
|
@@ -60,6 +60,20 @@ var CodeBlockView = exports.CodeBlockView = /*#__PURE__*/function () {
|
|
|
60
60
|
_this.lineNumberGutter.removeChild(_this.lineNumberGutter.lastChild);
|
|
61
61
|
}
|
|
62
62
|
}));
|
|
63
|
+
/**
|
|
64
|
+
* As the code block updates we get the maximum amount of digits in a line number and expand the number gutter to reflect this.
|
|
65
|
+
*/
|
|
66
|
+
(0, _defineProperty2.default)(this, "maintainDynamicGutterSize", function () {
|
|
67
|
+
var totalLineCount = 1;
|
|
68
|
+
_this.node.forEach(function (node) {
|
|
69
|
+
var text = node.text;
|
|
70
|
+
if (text) {
|
|
71
|
+
totalLineCount += (node.text.match(MATCH_NEWLINES) || []).length;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
var maxDigits = totalLineCount.toString().length;
|
|
75
|
+
_this.dom.style.setProperty('--lineNumberGutterWidth', "".concat(maxDigits, "ch"));
|
|
76
|
+
});
|
|
63
77
|
this.cleanupEditorDisabledListener = cleanupEditorDisabledListener;
|
|
64
78
|
var _DOMSerializer$render = _model.DOMSerializer.renderSpec(document, toDOM(_node, !(api !== null && api !== void 0 && (_api$editorDisabled = api.editorDisabled) !== null && _api$editorDisabled !== void 0 && (_api$editorDisabled = _api$editorDisabled.sharedState.currentState()) !== null && _api$editorDisabled !== void 0 && _api$editorDisabled.editorDisabled))),
|
|
65
79
|
dom = _DOMSerializer$render.dom,
|
|
@@ -71,7 +85,9 @@ var CodeBlockView = exports.CodeBlockView = /*#__PURE__*/function () {
|
|
|
71
85
|
this.contentDOM = contentDOM;
|
|
72
86
|
this.lineNumberGutter = this.dom.querySelector(".".concat((0, _platformFeatureFlags.fg)('editor_support_code_block_wrapping') ? _classNames.codeBlockClassNames.gutterFg : _classNames.codeBlockClassNames.gutter));
|
|
73
87
|
this.api = api;
|
|
74
|
-
if (
|
|
88
|
+
if ((0, _platformFeatureFlags.fg)('editor_support_code_block_wrapping')) {
|
|
89
|
+
this.maintainDynamicGutterSize();
|
|
90
|
+
} else {
|
|
75
91
|
this.ensureLineNumbers();
|
|
76
92
|
}
|
|
77
93
|
this.handleEditorDisabledChanged();
|
|
@@ -149,7 +165,9 @@ var CodeBlockView = exports.CodeBlockView = /*#__PURE__*/function () {
|
|
|
149
165
|
this.contentDOM.setAttribute('data-language', node.attrs.language || '');
|
|
150
166
|
}
|
|
151
167
|
this.node = node;
|
|
152
|
-
if (
|
|
168
|
+
if ((0, _platformFeatureFlags.fg)('editor_support_code_block_wrapping')) {
|
|
169
|
+
this.maintainDynamicGutterSize();
|
|
170
|
+
} else {
|
|
153
171
|
this.ensureLineNumbers();
|
|
154
172
|
}
|
|
155
173
|
if (_utils.browser.android) {
|
|
@@ -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,
|
|
73
|
+
var createLineDecoratorPluginState = function createLineDecoratorPluginState(pluginState, tr, state) {
|
|
63
74
|
var decorationSetFromState = pluginState.decorations;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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.
|
|
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,
|
|
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
|
}
|
|
@@ -176,7 +188,7 @@ var createPlugin = exports.createPlugin = function createPlugin(_ref) {
|
|
|
176
188
|
}
|
|
177
189
|
},
|
|
178
190
|
handleClickOn: (0, _selection.createSelectionClickHandler)(['codeBlock'], function (target) {
|
|
179
|
-
return !!(target.closest(".".concat(_classNames.codeBlockClassNames.gutter)) || target.classList.contains(_classNames.codeBlockClassNames.content));
|
|
191
|
+
return (0, _platformFeatureFlags.fg)('editor_support_code_block_wrapping') ? !!(target.classList.contains(_classNames.codeBlockClassNames.lineNumberWidget) || target.classList.contains(_classNames.codeBlockClassNames.gutterFg)) : !!(target.closest(".".concat(_classNames.codeBlockClassNames.gutter)) || target.classList.contains(_classNames.codeBlockClassNames.content));
|
|
180
192
|
}, {
|
|
181
193
|
useLongPressSelection: useLongPressSelection
|
|
182
194
|
}),
|
|
@@ -47,6 +47,20 @@ export class CodeBlockView {
|
|
|
47
47
|
this.lineNumberGutter.removeChild(this.lineNumberGutter.lastChild);
|
|
48
48
|
}
|
|
49
49
|
}));
|
|
50
|
+
/**
|
|
51
|
+
* As the code block updates we get the maximum amount of digits in a line number and expand the number gutter to reflect this.
|
|
52
|
+
*/
|
|
53
|
+
_defineProperty(this, "maintainDynamicGutterSize", () => {
|
|
54
|
+
let totalLineCount = 1;
|
|
55
|
+
this.node.forEach(node => {
|
|
56
|
+
const text = node.text;
|
|
57
|
+
if (text) {
|
|
58
|
+
totalLineCount += (node.text.match(MATCH_NEWLINES) || []).length;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
const maxDigits = totalLineCount.toString().length;
|
|
62
|
+
this.dom.style.setProperty('--lineNumberGutterWidth', `${maxDigits}ch`);
|
|
63
|
+
});
|
|
50
64
|
this.cleanupEditorDisabledListener = cleanupEditorDisabledListener;
|
|
51
65
|
const {
|
|
52
66
|
dom,
|
|
@@ -59,7 +73,9 @@ export class CodeBlockView {
|
|
|
59
73
|
this.contentDOM = contentDOM;
|
|
60
74
|
this.lineNumberGutter = this.dom.querySelector(`.${fg('editor_support_code_block_wrapping') ? codeBlockClassNames.gutterFg : codeBlockClassNames.gutter}`);
|
|
61
75
|
this.api = api;
|
|
62
|
-
if (
|
|
76
|
+
if (fg('editor_support_code_block_wrapping')) {
|
|
77
|
+
this.maintainDynamicGutterSize();
|
|
78
|
+
} else {
|
|
63
79
|
this.ensureLineNumbers();
|
|
64
80
|
}
|
|
65
81
|
this.handleEditorDisabledChanged();
|
|
@@ -126,7 +142,9 @@ export class CodeBlockView {
|
|
|
126
142
|
this.contentDOM.setAttribute('data-language', node.attrs.language || '');
|
|
127
143
|
}
|
|
128
144
|
this.node = node;
|
|
129
|
-
if (
|
|
145
|
+
if (fg('editor_support_code_block_wrapping')) {
|
|
146
|
+
this.maintainDynamicGutterSize();
|
|
147
|
+
} else {
|
|
130
148
|
this.ensureLineNumbers();
|
|
131
149
|
}
|
|
132
150
|
if (browser.android) {
|
|
@@ -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,
|
|
61
|
+
const createLineDecoratorPluginState = (pluginState, tr, state) => {
|
|
51
62
|
let decorationSetFromState = pluginState.decorations;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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.
|
|
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,
|
|
139
|
+
decorations: fg('editor_support_code_block_wrapping') ? createLineDecoratorPluginState(pluginState, tr, newState) : DecorationSet.empty
|
|
132
140
|
};
|
|
133
141
|
return newPluginState;
|
|
134
142
|
}
|
|
@@ -166,7 +174,7 @@ export const createPlugin = ({
|
|
|
166
174
|
nodeViews: {
|
|
167
175
|
codeBlock: (node, view, getPos) => codeBlockNodeView(node, view, getPos, api)
|
|
168
176
|
},
|
|
169
|
-
handleClickOn: createSelectionClickHandler(['codeBlock'], target => !!(target.closest(`.${codeBlockClassNames.gutter}`) || target.classList.contains(codeBlockClassNames.content)), {
|
|
177
|
+
handleClickOn: createSelectionClickHandler(['codeBlock'], target => fg('editor_support_code_block_wrapping') ? !!(target.classList.contains(codeBlockClassNames.lineNumberWidget) || target.classList.contains(codeBlockClassNames.gutterFg)) : !!(target.closest(`.${codeBlockClassNames.gutter}`) || target.classList.contains(codeBlockClassNames.content)), {
|
|
170
178
|
useLongPressSelection
|
|
171
179
|
}),
|
|
172
180
|
handleDOMEvents
|
|
@@ -53,6 +53,20 @@ export var CodeBlockView = /*#__PURE__*/function () {
|
|
|
53
53
|
_this.lineNumberGutter.removeChild(_this.lineNumberGutter.lastChild);
|
|
54
54
|
}
|
|
55
55
|
}));
|
|
56
|
+
/**
|
|
57
|
+
* As the code block updates we get the maximum amount of digits in a line number and expand the number gutter to reflect this.
|
|
58
|
+
*/
|
|
59
|
+
_defineProperty(this, "maintainDynamicGutterSize", function () {
|
|
60
|
+
var totalLineCount = 1;
|
|
61
|
+
_this.node.forEach(function (node) {
|
|
62
|
+
var text = node.text;
|
|
63
|
+
if (text) {
|
|
64
|
+
totalLineCount += (node.text.match(MATCH_NEWLINES) || []).length;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
var maxDigits = totalLineCount.toString().length;
|
|
68
|
+
_this.dom.style.setProperty('--lineNumberGutterWidth', "".concat(maxDigits, "ch"));
|
|
69
|
+
});
|
|
56
70
|
this.cleanupEditorDisabledListener = cleanupEditorDisabledListener;
|
|
57
71
|
var _DOMSerializer$render = DOMSerializer.renderSpec(document, toDOM(_node, !(api !== null && api !== void 0 && (_api$editorDisabled = api.editorDisabled) !== null && _api$editorDisabled !== void 0 && (_api$editorDisabled = _api$editorDisabled.sharedState.currentState()) !== null && _api$editorDisabled !== void 0 && _api$editorDisabled.editorDisabled))),
|
|
58
72
|
dom = _DOMSerializer$render.dom,
|
|
@@ -64,7 +78,9 @@ export var CodeBlockView = /*#__PURE__*/function () {
|
|
|
64
78
|
this.contentDOM = contentDOM;
|
|
65
79
|
this.lineNumberGutter = this.dom.querySelector(".".concat(fg('editor_support_code_block_wrapping') ? codeBlockClassNames.gutterFg : codeBlockClassNames.gutter));
|
|
66
80
|
this.api = api;
|
|
67
|
-
if (
|
|
81
|
+
if (fg('editor_support_code_block_wrapping')) {
|
|
82
|
+
this.maintainDynamicGutterSize();
|
|
83
|
+
} else {
|
|
68
84
|
this.ensureLineNumbers();
|
|
69
85
|
}
|
|
70
86
|
this.handleEditorDisabledChanged();
|
|
@@ -142,7 +158,9 @@ export var CodeBlockView = /*#__PURE__*/function () {
|
|
|
142
158
|
this.contentDOM.setAttribute('data-language', node.attrs.language || '');
|
|
143
159
|
}
|
|
144
160
|
this.node = node;
|
|
145
|
-
if (
|
|
161
|
+
if (fg('editor_support_code_block_wrapping')) {
|
|
162
|
+
this.maintainDynamicGutterSize();
|
|
163
|
+
} else {
|
|
146
164
|
this.ensureLineNumbers();
|
|
147
165
|
}
|
|
148
166
|
if (browser.android) {
|
|
@@ -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,
|
|
66
|
+
var createLineDecoratorPluginState = function createLineDecoratorPluginState(pluginState, tr, state) {
|
|
56
67
|
var decorationSetFromState = pluginState.decorations;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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.
|
|
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,
|
|
146
|
+
decorations: fg('editor_support_code_block_wrapping') ? createLineDecoratorPluginState(pluginState, tr, newState) : DecorationSet.empty
|
|
135
147
|
});
|
|
136
148
|
return newPluginState;
|
|
137
149
|
}
|
|
@@ -169,7 +181,7 @@ export var createPlugin = function createPlugin(_ref) {
|
|
|
169
181
|
}
|
|
170
182
|
},
|
|
171
183
|
handleClickOn: createSelectionClickHandler(['codeBlock'], function (target) {
|
|
172
|
-
return !!(target.closest(".".concat(codeBlockClassNames.gutter)) || target.classList.contains(codeBlockClassNames.content));
|
|
184
|
+
return fg('editor_support_code_block_wrapping') ? !!(target.classList.contains(codeBlockClassNames.lineNumberWidget) || target.classList.contains(codeBlockClassNames.gutterFg)) : !!(target.closest(".".concat(codeBlockClassNames.gutter)) || target.classList.contains(codeBlockClassNames.content));
|
|
173
185
|
}, {
|
|
174
186
|
useLongPressSelection: useLongPressSelection
|
|
175
187
|
}),
|
|
@@ -16,6 +16,10 @@ export declare class CodeBlockView {
|
|
|
16
16
|
updateDOMAndSelection(savedInnerHTML: string, newCursorPosition: number): void;
|
|
17
17
|
coalesceDOMElements(): void;
|
|
18
18
|
private ensureLineNumbers;
|
|
19
|
+
/**
|
|
20
|
+
* As the code block updates we get the maximum amount of digits in a line number and expand the number gutter to reflect this.
|
|
21
|
+
*/
|
|
22
|
+
private maintainDynamicGutterSize;
|
|
19
23
|
update(node: Node): boolean;
|
|
20
24
|
ignoreMutation(record: MutationRecord | {
|
|
21
25
|
type: 'selection';
|
|
@@ -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[];
|
|
@@ -16,6 +16,10 @@ export declare class CodeBlockView {
|
|
|
16
16
|
updateDOMAndSelection(savedInnerHTML: string, newCursorPosition: number): void;
|
|
17
17
|
coalesceDOMElements(): void;
|
|
18
18
|
private ensureLineNumbers;
|
|
19
|
+
/**
|
|
20
|
+
* As the code block updates we get the maximum amount of digits in a line number and expand the number gutter to reflect this.
|
|
21
|
+
*/
|
|
22
|
+
private maintainDynamicGutterSize;
|
|
19
23
|
update(node: Node): boolean;
|
|
20
24
|
ignoreMutation(record: MutationRecord | {
|
|
21
25
|
type: 'selection';
|
|
@@ -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.
|
|
3
|
+
"version": "3.3.4",
|
|
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.
|
|
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.
|
|
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",
|