@atlaskit/editor-plugin-code-block 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 +1 -0
- package/LICENSE.md +13 -0
- package/README.md +30 -0
- package/dist/cjs/actions.js +201 -0
- package/dist/cjs/ide-ux/bracket-handling.js +35 -0
- package/dist/cjs/ide-ux/commands.js +115 -0
- package/dist/cjs/ide-ux/line-handling.js +81 -0
- package/dist/cjs/ide-ux/paired-character-handling.js +23 -0
- package/dist/cjs/ide-ux/quote-handling.js +40 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/language-list.js +62 -0
- package/dist/cjs/nodeviews/code-block.js +153 -0
- package/dist/cjs/plugin-key.js +8 -0
- package/dist/cjs/plugin.js +120 -0
- package/dist/cjs/pm-plugins/actions.js +10 -0
- package/dist/cjs/pm-plugins/codeBlockCopySelectionPlugin.js +113 -0
- package/dist/cjs/pm-plugins/ide-ux.js +132 -0
- package/dist/cjs/pm-plugins/input-rule.js +68 -0
- package/dist/cjs/pm-plugins/keymaps.js +66 -0
- package/dist/cjs/pm-plugins/main-state.js +10 -0
- package/dist/cjs/pm-plugins/main.js +114 -0
- package/dist/cjs/refresh-browser-selection.js +29 -0
- package/dist/cjs/toolbar.js +131 -0
- package/dist/cjs/transform-to-code-block.js +84 -0
- package/dist/cjs/types.js +5 -0
- package/dist/cjs/ui/class-names.js +15 -0
- package/dist/cjs/utils.js +28 -0
- package/dist/es2019/actions.js +211 -0
- package/dist/es2019/ide-ux/bracket-handling.js +27 -0
- package/dist/es2019/ide-ux/commands.js +115 -0
- package/dist/es2019/ide-ux/line-handling.js +75 -0
- package/dist/es2019/ide-ux/paired-character-handling.js +12 -0
- package/dist/es2019/ide-ux/quote-handling.js +32 -0
- package/dist/es2019/index.js +1 -0
- package/dist/es2019/language-list.js +51 -0
- package/dist/es2019/nodeviews/code-block.js +126 -0
- package/dist/es2019/plugin-key.js +2 -0
- package/dist/es2019/plugin.js +104 -0
- package/dist/es2019/pm-plugins/actions.js +4 -0
- package/dist/es2019/pm-plugins/codeBlockCopySelectionPlugin.js +101 -0
- package/dist/es2019/pm-plugins/ide-ux.js +135 -0
- package/dist/es2019/pm-plugins/input-rule.js +60 -0
- package/dist/es2019/pm-plugins/keymaps.js +58 -0
- package/dist/es2019/pm-plugins/main-state.js +2 -0
- package/dist/es2019/pm-plugins/main.js +102 -0
- package/dist/es2019/refresh-browser-selection.js +25 -0
- package/dist/es2019/toolbar.js +108 -0
- package/dist/es2019/transform-to-code-block.js +79 -0
- package/dist/es2019/types.js +1 -0
- package/dist/es2019/ui/class-names.js +9 -0
- package/dist/es2019/utils.js +4 -0
- package/dist/esm/actions.js +191 -0
- package/dist/esm/ide-ux/bracket-handling.js +29 -0
- package/dist/esm/ide-ux/commands.js +107 -0
- package/dist/esm/ide-ux/line-handling.js +73 -0
- package/dist/esm/ide-ux/paired-character-handling.js +16 -0
- package/dist/esm/ide-ux/quote-handling.js +34 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/language-list.js +52 -0
- package/dist/esm/nodeviews/code-block.js +146 -0
- package/dist/esm/plugin-key.js +2 -0
- package/dist/esm/plugin.js +113 -0
- package/dist/esm/pm-plugins/actions.js +4 -0
- package/dist/esm/pm-plugins/codeBlockCopySelectionPlugin.js +103 -0
- package/dist/esm/pm-plugins/ide-ux.js +126 -0
- package/dist/esm/pm-plugins/input-rule.js +62 -0
- package/dist/esm/pm-plugins/keymaps.js +59 -0
- package/dist/esm/pm-plugins/main-state.js +4 -0
- package/dist/esm/pm-plugins/main.js +107 -0
- package/dist/esm/refresh-browser-selection.js +25 -0
- package/dist/esm/toolbar.js +121 -0
- package/dist/esm/transform-to-code-block.js +77 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/ui/class-names.js +9 -0
- package/dist/esm/utils.js +4 -0
- package/dist/types/actions.d.ts +18 -0
- package/dist/types/ide-ux/bracket-handling.d.ts +12 -0
- package/dist/types/ide-ux/commands.d.ts +7 -0
- package/dist/types/ide-ux/line-handling.d.ts +25 -0
- package/dist/types/ide-ux/paired-character-handling.d.ts +2 -0
- package/dist/types/ide-ux/quote-handling.d.ts +12 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/language-list.d.ts +942 -0
- package/dist/types/nodeviews/code-block.d.ts +21 -0
- package/dist/types/plugin-key.d.ts +2 -0
- package/dist/types/plugin.d.ts +19 -0
- package/dist/types/pm-plugins/actions.d.ts +4 -0
- package/dist/types/pm-plugins/codeBlockCopySelectionPlugin.d.ts +11 -0
- package/dist/types/pm-plugins/ide-ux.d.ts +5 -0
- package/dist/types/pm-plugins/input-rule.d.ts +3 -0
- package/dist/types/pm-plugins/keymaps.d.ts +4 -0
- package/dist/types/pm-plugins/main-state.d.ts +8 -0
- package/dist/types/pm-plugins/main.d.ts +10 -0
- package/dist/types/refresh-browser-selection.d.ts +5 -0
- package/dist/types/toolbar.d.ts +14 -0
- package/dist/types/transform-to-code-block.d.ts +3 -0
- package/dist/types/types.d.ts +6 -0
- package/dist/types/ui/class-names.d.ts +8 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types-ts4.5/actions.d.ts +18 -0
- package/dist/types-ts4.5/ide-ux/bracket-handling.d.ts +12 -0
- package/dist/types-ts4.5/ide-ux/commands.d.ts +7 -0
- package/dist/types-ts4.5/ide-ux/line-handling.d.ts +25 -0
- package/dist/types-ts4.5/ide-ux/paired-character-handling.d.ts +2 -0
- package/dist/types-ts4.5/ide-ux/quote-handling.d.ts +12 -0
- package/dist/types-ts4.5/index.d.ts +3 -0
- package/dist/types-ts4.5/language-list.d.ts +1641 -0
- package/dist/types-ts4.5/nodeviews/code-block.d.ts +21 -0
- package/dist/types-ts4.5/plugin-key.d.ts +2 -0
- package/dist/types-ts4.5/plugin.d.ts +19 -0
- package/dist/types-ts4.5/pm-plugins/actions.d.ts +4 -0
- package/dist/types-ts4.5/pm-plugins/codeBlockCopySelectionPlugin.d.ts +14 -0
- package/dist/types-ts4.5/pm-plugins/ide-ux.d.ts +5 -0
- package/dist/types-ts4.5/pm-plugins/input-rule.d.ts +3 -0
- package/dist/types-ts4.5/pm-plugins/keymaps.d.ts +4 -0
- package/dist/types-ts4.5/pm-plugins/main-state.d.ts +8 -0
- package/dist/types-ts4.5/pm-plugins/main.d.ts +10 -0
- package/dist/types-ts4.5/refresh-browser-selection.d.ts +5 -0
- package/dist/types-ts4.5/toolbar.d.ts +14 -0
- package/dist/types-ts4.5/transform-to-code-block.d.ts +3 -0
- package/dist/types-ts4.5/types.d.ts +6 -0
- package/dist/types-ts4.5/ui/class-names.d.ts +8 -0
- package/dist/types-ts4.5/utils.d.ts +4 -0
- package/package.json +99 -0
- package/report.api.md +73 -0
- package/tmp/api-report-tmp.d.ts +45 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.codeBlockNodeView = exports.CodeBlockView = void 0;
|
|
8
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
10
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
11
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
12
|
+
var _rafSchd = _interopRequireDefault(require("raf-schd"));
|
|
13
|
+
var _utils = require("@atlaskit/editor-common/utils");
|
|
14
|
+
var _model = require("@atlaskit/editor-prosemirror/model");
|
|
15
|
+
var _actions = require("../actions");
|
|
16
|
+
var _mainState = require("../pm-plugins/main-state");
|
|
17
|
+
var _classNames = require("../ui/class-names");
|
|
18
|
+
var MATCH_NEWLINES = new RegExp('\n', 'g');
|
|
19
|
+
var toDOM = function toDOM(node) {
|
|
20
|
+
return ['div', {
|
|
21
|
+
class: 'code-block'
|
|
22
|
+
}, ['div', {
|
|
23
|
+
class: _classNames.codeBlockClassNames.start,
|
|
24
|
+
contenteditable: 'false'
|
|
25
|
+
}], ['div', {
|
|
26
|
+
class: _classNames.codeBlockClassNames.contentWrapper
|
|
27
|
+
}, ['div', {
|
|
28
|
+
class: _classNames.codeBlockClassNames.gutter,
|
|
29
|
+
contenteditable: 'false'
|
|
30
|
+
}], ['div', {
|
|
31
|
+
class: _classNames.codeBlockClassNames.content
|
|
32
|
+
}, ['code', {
|
|
33
|
+
'data-language': node.attrs.language || '',
|
|
34
|
+
spellcheck: 'false',
|
|
35
|
+
contenteditable: 'true',
|
|
36
|
+
'data-testid': 'code-block--code'
|
|
37
|
+
}, 0]]], ['div', {
|
|
38
|
+
class: _classNames.codeBlockClassNames.end,
|
|
39
|
+
contenteditable: 'false'
|
|
40
|
+
}]];
|
|
41
|
+
};
|
|
42
|
+
var CodeBlockView = exports.CodeBlockView = /*#__PURE__*/function () {
|
|
43
|
+
function CodeBlockView(_node, view, getPos) {
|
|
44
|
+
var _this = this;
|
|
45
|
+
(0, _classCallCheck2.default)(this, CodeBlockView);
|
|
46
|
+
(0, _defineProperty2.default)(this, "ensureLineNumbers", (0, _rafSchd.default)(function () {
|
|
47
|
+
var lines = 1;
|
|
48
|
+
_this.node.forEach(function (node) {
|
|
49
|
+
var text = node.text;
|
|
50
|
+
if (text) {
|
|
51
|
+
lines += (node.text.match(MATCH_NEWLINES) || []).length;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
while (_this.lineNumberGutter.childElementCount < lines) {
|
|
55
|
+
_this.lineNumberGutter.appendChild(document.createElement('span'));
|
|
56
|
+
}
|
|
57
|
+
while (_this.lineNumberGutter.childElementCount > lines) {
|
|
58
|
+
_this.lineNumberGutter.removeChild(_this.lineNumberGutter.lastChild);
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
var _DOMSerializer$render = _model.DOMSerializer.renderSpec(document, toDOM(_node)),
|
|
62
|
+
dom = _DOMSerializer$render.dom,
|
|
63
|
+
contentDOM = _DOMSerializer$render.contentDOM;
|
|
64
|
+
this.getPos = getPos;
|
|
65
|
+
this.view = view;
|
|
66
|
+
this.node = _node;
|
|
67
|
+
this.dom = dom;
|
|
68
|
+
this.contentDOM = contentDOM;
|
|
69
|
+
this.lineNumberGutter = this.dom.querySelector(".".concat(_classNames.codeBlockClassNames.gutter));
|
|
70
|
+
this.ensureLineNumbers();
|
|
71
|
+
}
|
|
72
|
+
(0, _createClass2.default)(CodeBlockView, [{
|
|
73
|
+
key: "updateDOMAndSelection",
|
|
74
|
+
value: function updateDOMAndSelection(savedInnerHTML, newCursorPosition) {
|
|
75
|
+
var _this$dom;
|
|
76
|
+
if ((_this$dom = this.dom) !== null && _this$dom !== void 0 && _this$dom.childNodes && this.dom.childNodes.length > 1) {
|
|
77
|
+
var _contentView$childNod;
|
|
78
|
+
var contentWrapper = this.dom.childNodes[1];
|
|
79
|
+
var contentView = contentWrapper === null || contentWrapper === void 0 ? void 0 : contentWrapper.childNodes[1];
|
|
80
|
+
if ((contentView === null || contentView === void 0 || (_contentView$childNod = contentView.childNodes) === null || _contentView$childNod === void 0 ? void 0 : _contentView$childNod.length) > 0) {
|
|
81
|
+
var codeElement = contentView.firstChild;
|
|
82
|
+
codeElement.innerHTML = savedInnerHTML;
|
|
83
|
+
|
|
84
|
+
// We need to set cursor for the DOM update
|
|
85
|
+
var textElement = (0, _toConsumableArray2.default)(codeElement.childNodes).find(function (child) {
|
|
86
|
+
return child.nodeName === '#text';
|
|
87
|
+
});
|
|
88
|
+
var sel = window.getSelection();
|
|
89
|
+
var range = document.createRange();
|
|
90
|
+
range.setStart(textElement, newCursorPosition);
|
|
91
|
+
range.collapse(true);
|
|
92
|
+
sel === null || sel === void 0 || sel.removeAllRanges();
|
|
93
|
+
sel === null || sel === void 0 || sel.addRange(range);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}, {
|
|
98
|
+
key: "coalesceDOMElements",
|
|
99
|
+
value: function coalesceDOMElements() {
|
|
100
|
+
var _this$dom2;
|
|
101
|
+
if ((_this$dom2 = this.dom) !== null && _this$dom2 !== void 0 && _this$dom2.childNodes && this.dom.childNodes.length > 1) {
|
|
102
|
+
var contentWrapper = this.dom.childNodes[1];
|
|
103
|
+
var contentView = contentWrapper === null || contentWrapper === void 0 ? void 0 : contentWrapper.childNodes[1];
|
|
104
|
+
if (contentView !== null && contentView !== void 0 && contentView.childNodes && contentView.childNodes.length > 1) {
|
|
105
|
+
var savedInnerHTML = '';
|
|
106
|
+
while (contentView.childNodes.length > 1) {
|
|
107
|
+
var lastChild = contentView.lastChild;
|
|
108
|
+
savedInnerHTML = lastChild.innerHTML + savedInnerHTML;
|
|
109
|
+
contentView.removeChild(lastChild);
|
|
110
|
+
}
|
|
111
|
+
var firstChild = contentView.firstChild;
|
|
112
|
+
savedInnerHTML = firstChild.innerHTML + '\n' + savedInnerHTML;
|
|
113
|
+
var newCursorPosition = firstChild.innerHTML.length + 1;
|
|
114
|
+
setTimeout(this.updateDOMAndSelection.bind(this, savedInnerHTML, newCursorPosition), 20);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}, {
|
|
119
|
+
key: "update",
|
|
120
|
+
value: function update(node) {
|
|
121
|
+
if (node.type !== this.node.type) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (node !== this.node) {
|
|
125
|
+
if (node.attrs.language !== this.node.attrs.language) {
|
|
126
|
+
this.contentDOM.setAttribute('data-language', node.attrs.language || '');
|
|
127
|
+
}
|
|
128
|
+
this.node = node;
|
|
129
|
+
this.ensureLineNumbers();
|
|
130
|
+
if (_utils.browser.android) {
|
|
131
|
+
this.coalesceDOMElements();
|
|
132
|
+
(0, _actions.resetShouldIgnoreFollowingMutations)(this.view.state, this.view.dispatch);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}, {
|
|
138
|
+
key: "ignoreMutation",
|
|
139
|
+
value: function ignoreMutation(record) {
|
|
140
|
+
var pluginState = (0, _mainState.getPluginState)(this.view.state);
|
|
141
|
+
if (pluginState !== null && pluginState !== void 0 && pluginState.shouldIgnoreFollowingMutations) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Ensure updating the line-number gutter doesn't trigger reparsing the codeblock
|
|
146
|
+
return record.target === this.lineNumberGutter || record.target.parentNode === this.lineNumberGutter;
|
|
147
|
+
}
|
|
148
|
+
}]);
|
|
149
|
+
return CodeBlockView;
|
|
150
|
+
}();
|
|
151
|
+
var codeBlockNodeView = exports.codeBlockNodeView = function codeBlockNodeView(node, view, getPos) {
|
|
152
|
+
return new CodeBlockView(node, view, getPos);
|
|
153
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = void 0;
|
|
8
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
9
|
+
var _react = _interopRequireDefault(require("react"));
|
|
10
|
+
var _adfSchema = require("@atlaskit/adf-schema");
|
|
11
|
+
var _analytics = require("@atlaskit/editor-common/analytics");
|
|
12
|
+
var _messages = require("@atlaskit/editor-common/messages");
|
|
13
|
+
var _quickInsert = require("@atlaskit/editor-common/quick-insert");
|
|
14
|
+
var _actions = require("./actions");
|
|
15
|
+
var _codeBlockCopySelectionPlugin = require("./pm-plugins/codeBlockCopySelectionPlugin");
|
|
16
|
+
var _ideUx = _interopRequireDefault(require("./pm-plugins/ide-ux"));
|
|
17
|
+
var _inputRule = require("./pm-plugins/input-rule");
|
|
18
|
+
var _keymaps = _interopRequireDefault(require("./pm-plugins/keymaps"));
|
|
19
|
+
var _main = require("./pm-plugins/main");
|
|
20
|
+
var _refreshBrowserSelection = _interopRequireDefault(require("./refresh-browser-selection"));
|
|
21
|
+
var _toolbar = require("./toolbar");
|
|
22
|
+
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; }
|
|
23
|
+
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; }
|
|
24
|
+
var codeBlockPlugin = function codeBlockPlugin(_ref) {
|
|
25
|
+
var options = _ref.config,
|
|
26
|
+
api = _ref.api;
|
|
27
|
+
return {
|
|
28
|
+
name: 'codeBlock',
|
|
29
|
+
nodes: function nodes() {
|
|
30
|
+
return [{
|
|
31
|
+
name: 'codeBlock',
|
|
32
|
+
node: _adfSchema.codeBlock
|
|
33
|
+
}];
|
|
34
|
+
},
|
|
35
|
+
pmPlugins: function pmPlugins() {
|
|
36
|
+
return [{
|
|
37
|
+
name: 'codeBlock',
|
|
38
|
+
plugin: function plugin(_ref2) {
|
|
39
|
+
var _options$appearance;
|
|
40
|
+
var getIntl = _ref2.getIntl;
|
|
41
|
+
return (0, _main.createPlugin)(_objectSpread(_objectSpread({}, options), {}, {
|
|
42
|
+
getIntl: getIntl,
|
|
43
|
+
appearance: (_options$appearance = options === null || options === void 0 ? void 0 : options.appearance) !== null && _options$appearance !== void 0 ? _options$appearance : 'comment'
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
}, {
|
|
47
|
+
name: 'codeBlockInputRule',
|
|
48
|
+
plugin: function plugin(_ref3) {
|
|
49
|
+
var _api$analytics;
|
|
50
|
+
var schema = _ref3.schema;
|
|
51
|
+
return (0, _inputRule.createCodeBlockInputRule)(schema, api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions);
|
|
52
|
+
}
|
|
53
|
+
}, {
|
|
54
|
+
name: 'codeBlockIDEKeyBindings',
|
|
55
|
+
plugin: function plugin() {
|
|
56
|
+
return (0, _ideUx.default)(api);
|
|
57
|
+
}
|
|
58
|
+
}, {
|
|
59
|
+
name: 'codeBlockKeyMap',
|
|
60
|
+
plugin: function plugin(_ref4) {
|
|
61
|
+
var schema = _ref4.schema;
|
|
62
|
+
return (0, _keymaps.default)(schema);
|
|
63
|
+
}
|
|
64
|
+
}, {
|
|
65
|
+
name: 'codeBlockCopySelection',
|
|
66
|
+
plugin: function plugin() {
|
|
67
|
+
return (0, _codeBlockCopySelectionPlugin.codeBlockCopySelectionPlugin)();
|
|
68
|
+
}
|
|
69
|
+
}];
|
|
70
|
+
},
|
|
71
|
+
// Workaround for a firefox issue where dom selection is off sync
|
|
72
|
+
// https://product-fabric.atlassian.net/browse/ED-12442
|
|
73
|
+
onEditorViewStateUpdated: function onEditorViewStateUpdated(props) {
|
|
74
|
+
(0, _refreshBrowserSelection.default)(props.originalTransaction, props.newEditorState);
|
|
75
|
+
},
|
|
76
|
+
actions: {
|
|
77
|
+
/*
|
|
78
|
+
* Function will insert code block at current selection if block is empty or below current selection and set focus on it.
|
|
79
|
+
*/
|
|
80
|
+
insertCodeBlock: function insertCodeBlock(inputMethod) {
|
|
81
|
+
var _api$analytics2;
|
|
82
|
+
return (0, _actions.insertCodeBlockWithAnalytics)(inputMethod, api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions);
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
pluginsOptions: {
|
|
86
|
+
quickInsert: function quickInsert(_ref5) {
|
|
87
|
+
var formatMessage = _ref5.formatMessage;
|
|
88
|
+
return [{
|
|
89
|
+
id: 'codeblock',
|
|
90
|
+
title: formatMessage(_messages.blockTypeMessages.codeblock),
|
|
91
|
+
description: formatMessage(_messages.blockTypeMessages.codeblockDescription),
|
|
92
|
+
keywords: ['code block'],
|
|
93
|
+
priority: 700,
|
|
94
|
+
keyshortcut: '```',
|
|
95
|
+
icon: function icon() {
|
|
96
|
+
return /*#__PURE__*/_react.default.createElement(_quickInsert.IconCode, null);
|
|
97
|
+
},
|
|
98
|
+
action: function action(_insert, state) {
|
|
99
|
+
var _api$analytics3;
|
|
100
|
+
var tr = (0, _actions.createInsertCodeBlockTransaction)({
|
|
101
|
+
state: state
|
|
102
|
+
});
|
|
103
|
+
api === null || api === void 0 || (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 || _api$analytics3.actions.attachAnalyticsEvent({
|
|
104
|
+
action: _analytics.ACTION.INSERTED,
|
|
105
|
+
actionSubject: _analytics.ACTION_SUBJECT.DOCUMENT,
|
|
106
|
+
actionSubjectId: _analytics.ACTION_SUBJECT_ID.CODE_BLOCK,
|
|
107
|
+
attributes: {
|
|
108
|
+
inputMethod: _analytics.INPUT_METHOD.QUICK_INSERT
|
|
109
|
+
},
|
|
110
|
+
eventType: _analytics.EVENT_TYPE.TRACK
|
|
111
|
+
})(tr);
|
|
112
|
+
return tr;
|
|
113
|
+
}
|
|
114
|
+
}];
|
|
115
|
+
},
|
|
116
|
+
floatingToolbar: (0, _toolbar.getToolbarConfig)(options === null || options === void 0 ? void 0 : options.allowCopyToClipboard, api)
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
var _default = exports.default = codeBlockPlugin;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ACTIONS = void 0;
|
|
7
|
+
var ACTIONS = exports.ACTIONS = {
|
|
8
|
+
SET_COPIED_TO_CLIPBOARD: 'SET_COPIED_TO_CLIPBOARD',
|
|
9
|
+
SET_SHOULD_IGNORE_FOLLOWING_MUTATIONS: 'SET_SHOULD_IGNORE_FOLLOWING_MUTATIONS'
|
|
10
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.codeBlockCopySelectionPlugin = codeBlockCopySelectionPlugin;
|
|
8
|
+
exports.copySelectionPluginKey = void 0;
|
|
9
|
+
exports.provideVisualFeedbackForCopyButton = provideVisualFeedbackForCopyButton;
|
|
10
|
+
exports.removeVisualFeedbackForCopyButton = removeVisualFeedbackForCopyButton;
|
|
11
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
12
|
+
var _copyButton = require("@atlaskit/editor-common/copy-button");
|
|
13
|
+
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
|
|
14
|
+
var _state = require("@atlaskit/editor-prosemirror/state");
|
|
15
|
+
var _view = require("@atlaskit/editor-prosemirror/view");
|
|
16
|
+
var copySelectionPluginKey = exports.copySelectionPluginKey = new _state.PluginKey('codeBlockCopySelectionPlugin');
|
|
17
|
+
function getSelectionDecorationStartAndEnd(_ref) {
|
|
18
|
+
var state = _ref.state,
|
|
19
|
+
transaction = _ref.transaction;
|
|
20
|
+
var codeBlockNode = (0, _copyButton.getSelectedNodeOrNodeParentByNodeType)({
|
|
21
|
+
nodeType: state.schema.nodes.codeBlock,
|
|
22
|
+
selection: transaction.selection
|
|
23
|
+
});
|
|
24
|
+
if (!codeBlockNode) {
|
|
25
|
+
return {
|
|
26
|
+
decorationStartAndEnd: undefined
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
var decorationStartAndEnd = [codeBlockNode.start, codeBlockNode.start + codeBlockNode.node.nodeSize];
|
|
30
|
+
return {
|
|
31
|
+
decorationStartAndEnd: decorationStartAndEnd
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function codeBlockCopySelectionPlugin() {
|
|
35
|
+
return new _safePlugin.SafePlugin({
|
|
36
|
+
key: copySelectionPluginKey,
|
|
37
|
+
state: {
|
|
38
|
+
init: function init() {
|
|
39
|
+
return {
|
|
40
|
+
decorationStartAndEnd: undefined
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
apply: function apply(transaction, currentCodeBlockCopySelectionPluginState, _oldState, newState) {
|
|
44
|
+
switch (transaction.getMeta(copySelectionPluginKey)) {
|
|
45
|
+
case 'show-selection':
|
|
46
|
+
{
|
|
47
|
+
return getSelectionDecorationStartAndEnd({
|
|
48
|
+
state: newState,
|
|
49
|
+
transaction: transaction
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
case 'remove-selection':
|
|
53
|
+
return {
|
|
54
|
+
decorationStartAndEnd: undefined
|
|
55
|
+
};
|
|
56
|
+
default:
|
|
57
|
+
// The contents of the code block can change while the selection is being shown
|
|
58
|
+
// (either from collab edits -- or from the user continuing to type while hovering
|
|
59
|
+
// the mouse over the copy button).
|
|
60
|
+
// This ensures the selection is updated in these cases.
|
|
61
|
+
if (currentCodeBlockCopySelectionPluginState.decorationStartAndEnd !== undefined) {
|
|
62
|
+
return getSelectionDecorationStartAndEnd({
|
|
63
|
+
state: newState,
|
|
64
|
+
transaction: transaction
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return currentCodeBlockCopySelectionPluginState;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
props: {
|
|
72
|
+
decorations: function decorations(state) {
|
|
73
|
+
if (copySelectionPluginKey.getState(state).decorationStartAndEnd) {
|
|
74
|
+
var _copySelectionPluginK = (0, _slicedToArray2.default)(copySelectionPluginKey.getState(state).decorationStartAndEnd, 2),
|
|
75
|
+
_start = _copySelectionPluginK[0],
|
|
76
|
+
_end = _copySelectionPluginK[1];
|
|
77
|
+
return _view.DecorationSet.create(state.doc, [_view.Decoration.inline(_start, _end, {
|
|
78
|
+
class: 'ProseMirror-fake-text-selection'
|
|
79
|
+
})]);
|
|
80
|
+
}
|
|
81
|
+
return _view.DecorationSet.empty;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function provideVisualFeedbackForCopyButton(state, dispatch) {
|
|
87
|
+
var tr = state.tr;
|
|
88
|
+
tr.setMeta(copySelectionPluginKey, 'show-selection');
|
|
89
|
+
|
|
90
|
+
// note: dispatch should always be defined when called from the
|
|
91
|
+
// floating toolbar. Howver the Command type which the floating toolbar
|
|
92
|
+
// uses suggests it's optional.
|
|
93
|
+
// Using the type here to protect against future refactors of the
|
|
94
|
+
// floating toolbar
|
|
95
|
+
if (dispatch) {
|
|
96
|
+
dispatch(tr);
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
function removeVisualFeedbackForCopyButton(state, dispatch) {
|
|
101
|
+
var tr = state.tr;
|
|
102
|
+
tr.setMeta(copySelectionPluginKey, 'remove-selection');
|
|
103
|
+
|
|
104
|
+
// note: dispatch should always be defined when called from the
|
|
105
|
+
// floating toolbar. Howver the Command type which the floating toolbar
|
|
106
|
+
// uses suggests it's optional.
|
|
107
|
+
// Using the type here to protect against future refactors of the
|
|
108
|
+
// floating toolbar
|
|
109
|
+
if (dispatch) {
|
|
110
|
+
dispatch(tr);
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
|
|
8
|
+
var _utils = require("@atlaskit/editor-common/utils");
|
|
9
|
+
var _keymap = require("@atlaskit/editor-prosemirror/keymap");
|
|
10
|
+
var _state = require("@atlaskit/editor-prosemirror/state");
|
|
11
|
+
var _utils2 = require("@atlaskit/editor-prosemirror/utils");
|
|
12
|
+
var _bracketHandling = require("../ide-ux/bracket-handling");
|
|
13
|
+
var _commands = require("../ide-ux/commands");
|
|
14
|
+
var _lineHandling = require("../ide-ux/line-handling");
|
|
15
|
+
var _pairedCharacterHandling = require("../ide-ux/paired-character-handling");
|
|
16
|
+
var _quoteHandling = require("../ide-ux/quote-handling");
|
|
17
|
+
var _utils3 = require("../utils");
|
|
18
|
+
var ideUX = function ideUX(pluginInjectionApi) {
|
|
19
|
+
var _pluginInjectionApi$a;
|
|
20
|
+
var editorAnalyticsAPI = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a === void 0 ? void 0 : _pluginInjectionApi$a.actions;
|
|
21
|
+
return new _safePlugin.SafePlugin({
|
|
22
|
+
props: {
|
|
23
|
+
handleTextInput: function handleTextInput(view, from, to, text) {
|
|
24
|
+
var _pluginInjectionApi$c;
|
|
25
|
+
var state = view.state,
|
|
26
|
+
dispatch = view.dispatch;
|
|
27
|
+
var compositionPluginState = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$c = pluginInjectionApi.composition) === null || _pluginInjectionApi$c === void 0 ? void 0 : _pluginInjectionApi$c.sharedState.currentState();
|
|
28
|
+
if ((0, _lineHandling.isCursorInsideCodeBlock)(state) && !(compositionPluginState !== null && compositionPluginState !== void 0 && compositionPluginState.isComposing)) {
|
|
29
|
+
var beforeText = (0, _lineHandling.getStartOfCurrentLine)(state).text;
|
|
30
|
+
var afterText = (0, _lineHandling.getEndOfCurrentLine)(state).text;
|
|
31
|
+
|
|
32
|
+
// If text is a closing bracket/quote and we've already inserted it, move the selection after
|
|
33
|
+
if ((0, _pairedCharacterHandling.isCursorBeforeClosingCharacter)(afterText) && (0, _pairedCharacterHandling.isClosingCharacter)(text) && afterText.startsWith(text)) {
|
|
34
|
+
dispatch((0, _utils2.setTextSelection)(to + text.length)(state.tr));
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Automatically add right-hand side bracket when user types the left bracket
|
|
39
|
+
if ((0, _bracketHandling.shouldAutoCloseBracket)(beforeText, afterText)) {
|
|
40
|
+
var _getAutoClosingBracke = (0, _bracketHandling.getAutoClosingBracketInfo)(beforeText + text, afterText),
|
|
41
|
+
left = _getAutoClosingBracke.left,
|
|
42
|
+
right = _getAutoClosingBracke.right;
|
|
43
|
+
if (left && right) {
|
|
44
|
+
var bracketPair = state.schema.text(text + right);
|
|
45
|
+
var tr = state.tr.replaceWith(from, to, bracketPair);
|
|
46
|
+
dispatch((0, _utils2.setTextSelection)(from + text.length)(tr));
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Automatically add closing quote when user types a starting quote
|
|
52
|
+
if ((0, _quoteHandling.shouldAutoCloseQuote)(beforeText, afterText)) {
|
|
53
|
+
var _getAutoClosingQuoteI = (0, _quoteHandling.getAutoClosingQuoteInfo)(beforeText + text, afterText),
|
|
54
|
+
leftQuote = _getAutoClosingQuoteI.left,
|
|
55
|
+
rightQuote = _getAutoClosingQuoteI.right;
|
|
56
|
+
if (leftQuote && rightQuote) {
|
|
57
|
+
var quotePair = state.schema.text(text + rightQuote);
|
|
58
|
+
var _tr = state.tr.replaceWith(from, to, quotePair);
|
|
59
|
+
dispatch((0, _utils2.setTextSelection)(from + text.length)(_tr));
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
},
|
|
66
|
+
handleKeyDown: (0, _keymap.keydownHandler)({
|
|
67
|
+
Backspace: function Backspace(state, dispatch) {
|
|
68
|
+
if ((0, _lineHandling.isCursorInsideCodeBlock)(state)) {
|
|
69
|
+
var $cursor = (0, _utils3.getCursor)(state.selection);
|
|
70
|
+
var beforeText = (0, _lineHandling.getStartOfCurrentLine)(state).text;
|
|
71
|
+
var afterText = (0, _lineHandling.getEndOfCurrentLine)(state).text;
|
|
72
|
+
var _getAutoClosingBracke2 = (0, _bracketHandling.getAutoClosingBracketInfo)(beforeText, afterText),
|
|
73
|
+
leftBracket = _getAutoClosingBracke2.left,
|
|
74
|
+
rightBracket = _getAutoClosingBracke2.right,
|
|
75
|
+
hasTrailingMatchingBracket = _getAutoClosingBracke2.hasTrailingMatchingBracket;
|
|
76
|
+
if (leftBracket && rightBracket && hasTrailingMatchingBracket && dispatch) {
|
|
77
|
+
dispatch(state.tr.delete($cursor.pos - leftBracket.length, $cursor.pos + rightBracket.length));
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
var _getAutoClosingQuoteI2 = (0, _quoteHandling.getAutoClosingQuoteInfo)(beforeText, afterText),
|
|
81
|
+
leftQuote = _getAutoClosingQuoteI2.left,
|
|
82
|
+
rightQuote = _getAutoClosingQuoteI2.right,
|
|
83
|
+
hasTrailingMatchingQuote = _getAutoClosingQuoteI2.hasTrailingMatchingQuote;
|
|
84
|
+
if (leftQuote && rightQuote && hasTrailingMatchingQuote && dispatch) {
|
|
85
|
+
dispatch(state.tr.delete($cursor.pos - leftQuote.length, $cursor.pos + rightQuote.length));
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
var _getLineInfo = (0, _lineHandling.getLineInfo)(beforeText),
|
|
89
|
+
_getLineInfo$indentTo = _getLineInfo.indentToken,
|
|
90
|
+
size = _getLineInfo$indentTo.size,
|
|
91
|
+
token = _getLineInfo$indentTo.token,
|
|
92
|
+
indentText = _getLineInfo.indentText;
|
|
93
|
+
if (beforeText === indentText) {
|
|
94
|
+
if (indentText.endsWith(token.repeat(size)) && dispatch) {
|
|
95
|
+
dispatch(state.tr.delete($cursor.pos - (size - indentText.length % size || size), $cursor.pos));
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
},
|
|
102
|
+
Enter: (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, _commands.insertNewlineWithIndent),
|
|
103
|
+
'Mod-]': (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, (0, _commands.indent)(editorAnalyticsAPI)),
|
|
104
|
+
'Mod-[': (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, (0, _commands.outdent)(editorAnalyticsAPI)),
|
|
105
|
+
Tab: (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, function (state, dispatch) {
|
|
106
|
+
if (!dispatch) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
if ((0, _lineHandling.isCursorInsideCodeBlock)(state)) {
|
|
110
|
+
return (0, _commands.insertIndent)(state, dispatch);
|
|
111
|
+
}
|
|
112
|
+
return (0, _commands.indent)(editorAnalyticsAPI)(state, dispatch);
|
|
113
|
+
}),
|
|
114
|
+
'Shift-Tab': (0, _utils.filterCommand)(_lineHandling.isSelectionEntirelyInsideCodeBlock, (0, _commands.outdent)(editorAnalyticsAPI)),
|
|
115
|
+
'Mod-a': function ModA(state, dispatch) {
|
|
116
|
+
if ((0, _lineHandling.isSelectionEntirelyInsideCodeBlock)(state)) {
|
|
117
|
+
var _state$selection = state.selection,
|
|
118
|
+
$from = _state$selection.$from,
|
|
119
|
+
$to = _state$selection.$to;
|
|
120
|
+
var isFullCodeBlockSelection = $from.parentOffset === 0 && $to.parentOffset === $to.parent.nodeSize - 2;
|
|
121
|
+
if (!isFullCodeBlockSelection && dispatch) {
|
|
122
|
+
dispatch(state.tr.setSelection(_state.TextSelection.create(state.doc, $from.start(), $to.end())));
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
var _default = exports.default = ideUX;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createCodeBlockInputRule = createCodeBlockInputRule;
|
|
7
|
+
var _analytics = require("@atlaskit/editor-common/analytics");
|
|
8
|
+
var _commands = require("@atlaskit/editor-common/commands");
|
|
9
|
+
var _utils = require("@atlaskit/editor-common/utils");
|
|
10
|
+
var _utils2 = require("@atlaskit/editor-prosemirror/utils");
|
|
11
|
+
var _prosemirrorInputRules = require("@atlaskit/prosemirror-input-rules");
|
|
12
|
+
var _transformToCodeBlock = require("../transform-to-code-block");
|
|
13
|
+
function createCodeBlockInputRule(schema, editorAnalyticsAPI) {
|
|
14
|
+
var rules = getCodeBlockRules(editorAnalyticsAPI, schema);
|
|
15
|
+
return (0, _prosemirrorInputRules.createPlugin)('code-block-input-rule', rules, {
|
|
16
|
+
isBlockNodeRule: true
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get all code block input rules
|
|
22
|
+
*
|
|
23
|
+
* @param {Schema} schema
|
|
24
|
+
* @returns {InputRuleWithHandler[]}
|
|
25
|
+
*/
|
|
26
|
+
function getCodeBlockRules(editorAnalyticsAPI, schema) {
|
|
27
|
+
var ruleAnalytics = (0, _utils.inputRuleWithAnalytics)({
|
|
28
|
+
action: _analytics.ACTION.INSERTED,
|
|
29
|
+
actionSubject: _analytics.ACTION_SUBJECT.DOCUMENT,
|
|
30
|
+
actionSubjectId: _analytics.ACTION_SUBJECT_ID.CODE_BLOCK,
|
|
31
|
+
attributes: {
|
|
32
|
+
inputMethod: _analytics.INPUT_METHOD.FORMATTING
|
|
33
|
+
},
|
|
34
|
+
eventType: _analytics.EVENT_TYPE.TRACK
|
|
35
|
+
}, editorAnalyticsAPI);
|
|
36
|
+
var validMatchLength = function validMatchLength(match) {
|
|
37
|
+
return match.length > 0 && match[0].length === 3;
|
|
38
|
+
};
|
|
39
|
+
var threeTildeRule = (0, _prosemirrorInputRules.createRule)(/(?!\s)(`{3,})$/, function (state, match, start, end) {
|
|
40
|
+
if (!validMatchLength(match)) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
var attributes = {};
|
|
44
|
+
if (match[4]) {
|
|
45
|
+
attributes.language = match[4];
|
|
46
|
+
}
|
|
47
|
+
if ((0, _transformToCodeBlock.isConvertableToCodeBlock)(state)) {
|
|
48
|
+
return (0, _transformToCodeBlock.transformToCodeBlockAction)(state, start, attributes);
|
|
49
|
+
}
|
|
50
|
+
var tr = state.tr;
|
|
51
|
+
tr.delete(start, end);
|
|
52
|
+
var codeBlock = tr.doc.type.schema.nodes.codeBlock.createChecked();
|
|
53
|
+
(0, _utils2.safeInsert)(codeBlock)(tr);
|
|
54
|
+
return tr;
|
|
55
|
+
});
|
|
56
|
+
var leftNodeReplacementThreeTildeRule = (0, _prosemirrorInputRules.createRule)(new RegExp("((".concat(_prosemirrorInputRules.leafNodeReplacementCharacter, "`{3,})|^\\s(`{3,}))(\\S*)$")), function (state, match, start, end) {
|
|
57
|
+
if (!validMatchLength(match)) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
var attributes = {};
|
|
61
|
+
if (match[4]) {
|
|
62
|
+
attributes.language = match[4];
|
|
63
|
+
}
|
|
64
|
+
var inlineStart = Math.max(match.index + state.selection.$from.start(), 1);
|
|
65
|
+
return (0, _commands.insertBlock)(state, schema.nodes.codeBlock, inlineStart, end, attributes);
|
|
66
|
+
});
|
|
67
|
+
return [ruleAnalytics(threeTildeRule), ruleAnalytics(leftNodeReplacementThreeTildeRule)];
|
|
68
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
exports.keymapPlugin = keymapPlugin;
|
|
8
|
+
var _utils = require("@atlaskit/editor-common/utils");
|
|
9
|
+
var _keymap = require("@atlaskit/editor-prosemirror/keymap");
|
|
10
|
+
var _state = require("@atlaskit/editor-prosemirror/state");
|
|
11
|
+
var _utils2 = require("@atlaskit/editor-prosemirror/utils");
|
|
12
|
+
var _utils3 = require("../utils");
|
|
13
|
+
var deleteCurrentItem = function deleteCurrentItem($from) {
|
|
14
|
+
return function (tr) {
|
|
15
|
+
return tr.delete($from.before($from.depth), $from.after($from.depth));
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
var setTextSelection = function setTextSelection(pos) {
|
|
19
|
+
return function (tr) {
|
|
20
|
+
var newSelection = _state.Selection.findFrom(tr.doc.resolve(pos), -1, true);
|
|
21
|
+
if (newSelection) {
|
|
22
|
+
tr.setSelection(newSelection);
|
|
23
|
+
}
|
|
24
|
+
return tr;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
function keymapPlugin(schema) {
|
|
28
|
+
return (0, _keymap.keymap)({
|
|
29
|
+
Backspace: function Backspace(state, dispatch) {
|
|
30
|
+
var $cursor = (0, _utils3.getCursor)(state.selection);
|
|
31
|
+
var _state$schema$nodes = state.schema.nodes,
|
|
32
|
+
paragraph = _state$schema$nodes.paragraph,
|
|
33
|
+
codeBlock = _state$schema$nodes.codeBlock,
|
|
34
|
+
listItem = _state$schema$nodes.listItem,
|
|
35
|
+
table = _state$schema$nodes.table,
|
|
36
|
+
layoutColumn = _state$schema$nodes.layoutColumn;
|
|
37
|
+
if (!$cursor || $cursor.parent.type !== codeBlock || !dispatch) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if ($cursor.pos === 1 || (0, _utils2.hasParentNodeOfType)(listItem)(state.selection) && $cursor.parentOffset === 0) {
|
|
41
|
+
var node = (0, _utils2.findParentNodeOfTypeClosestToPos)($cursor, codeBlock);
|
|
42
|
+
if (!node) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
dispatch(state.tr.setNodeMarkup(node.pos, node.node.type, node.node.attrs, []).setBlockType($cursor.pos, $cursor.pos, paragraph));
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
if ($cursor.node && (0, _utils.isEmptyNode)(schema)($cursor.node()) && ((0, _utils2.hasParentNodeOfType)(layoutColumn)(state.selection) || (0, _utils2.hasParentNodeOfType)(table)(state.selection))) {
|
|
49
|
+
var tr = state.tr;
|
|
50
|
+
var insertPos = $cursor.pos;
|
|
51
|
+
deleteCurrentItem($cursor)(tr);
|
|
52
|
+
setTextSelection(insertPos)(tr);
|
|
53
|
+
dispatch(tr.scrollIntoView());
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Handle not nested empty code block
|
|
58
|
+
if ((0, _utils.isEmptyNode)(schema)($cursor.node())) {
|
|
59
|
+
dispatch(deleteCurrentItem($cursor)(state === null || state === void 0 ? void 0 : state.tr));
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
var _default = exports.default = keymapPlugin;
|