@atlaskit/prosemirror-input-rules 2.2.4 → 2.3.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 +16 -0
- package/dist/cjs/index.js +14 -1
- package/dist/cjs/utils.js +67 -0
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/index.js +2 -1
- package/dist/es2019/utils.js +59 -0
- package/dist/es2019/version.json +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/utils.js +59 -0
- package/dist/esm/version.json +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/utils.d.ts +9 -0
- package/dist/types-ts4.5/index.d.ts +1 -0
- package/dist/types-ts4.5/utils.d.ts +9 -0
- package/package.json +5 -7
- package/report.api.md +19 -0
- package/src/__tests__/unit/handler.ts +2 -2
- package/src/index.ts +2 -0
- package/src/utils.ts +95 -0
- package/tmp/api-report-tmp.d.ts +0 -58
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @atlaskit/prosemirror-input-rules
|
|
2
2
|
|
|
3
|
+
## 2.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`8b891bf3590`](https://bitbucket.org/atlassian/atlassian-frontend/commits/8b891bf3590) - This change introduces `editor-plugin-hyperlink` which separates the hyperlink plugin from `editor-core`. In order to enable this change there are now new entry points on `editor-common` (such as `/link`, `/quick-insert`) in order to separate common code. Further `prosemirror-input-rules` now has new exports of `createPlugin` and `createRule` which are used in many plugins in `editor-core`.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies
|
|
12
|
+
|
|
13
|
+
## 2.2.5
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- [`5f5ba16de66`](https://bitbucket.org/atlassian/atlassian-frontend/commits/5f5ba16de66) - [ED-13910] Fix prosemirror types
|
|
18
|
+
|
|
3
19
|
## 2.2.4
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/dist/cjs/index.js
CHANGED
|
@@ -21,6 +21,18 @@ Object.defineProperty(exports, "createInputRulePlugin", {
|
|
|
21
21
|
return _plugin.createInputRulePlugin;
|
|
22
22
|
}
|
|
23
23
|
});
|
|
24
|
+
Object.defineProperty(exports, "createPlugin", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function get() {
|
|
27
|
+
return _utils.createPlugin;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(exports, "createRule", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function get() {
|
|
33
|
+
return _utils.createRule;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
24
36
|
Object.defineProperty(exports, "leafNodeReplacementCharacter", {
|
|
25
37
|
enumerable: true,
|
|
26
38
|
get: function get() {
|
|
@@ -28,4 +40,5 @@ Object.defineProperty(exports, "leafNodeReplacementCharacter", {
|
|
|
28
40
|
}
|
|
29
41
|
});
|
|
30
42
|
var _constants = require("./constants");
|
|
31
|
-
var _plugin = require("./plugin");
|
|
43
|
+
var _plugin = require("./plugin");
|
|
44
|
+
var _utils = require("./utils");
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createRule = exports.createPlugin = void 0;
|
|
7
|
+
var _prosemirrorHistory = require("prosemirror-history");
|
|
8
|
+
var _prosemirrorState = require("prosemirror-state");
|
|
9
|
+
var _selection = require("@atlaskit/editor-common/selection");
|
|
10
|
+
var _plugin = require("./plugin");
|
|
11
|
+
var createRule = function createRule(match, handler) {
|
|
12
|
+
return {
|
|
13
|
+
match: match,
|
|
14
|
+
handler: handler,
|
|
15
|
+
onHandlerApply: function onHandlerApply(state, tr) {
|
|
16
|
+
(0, _prosemirrorHistory.closeHistory)(tr);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
exports.createRule = createRule;
|
|
21
|
+
var hasUnsupportedMarks = function hasUnsupportedMarks(state, start, end, marksNameUnsupported) {
|
|
22
|
+
var isUnsupportedMark = function isUnsupportedMark(node) {
|
|
23
|
+
return (marksNameUnsupported || []).includes(node.type.name);
|
|
24
|
+
};
|
|
25
|
+
var $from = state.doc.resolve(start);
|
|
26
|
+
var $to = state.doc.resolve(end);
|
|
27
|
+
var marksInSelection = start === end ? $from.marks() : $from.marksAcross($to);
|
|
28
|
+
return (marksInSelection || []).some(isUnsupportedMark);
|
|
29
|
+
};
|
|
30
|
+
var isCursorInsideUnsupportedMarks = function isCursorInsideUnsupportedMarks(state, marksNameUnsupported) {
|
|
31
|
+
var _$cursor$nodeBefore, _$cursor$nodeBefore$m;
|
|
32
|
+
var selection = state.selection;
|
|
33
|
+
if (!(selection instanceof _prosemirrorState.TextSelection)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
var $cursor = selection.$cursor;
|
|
37
|
+
var isUnsupportedMark = function isUnsupportedMark(node) {
|
|
38
|
+
return marksNameUnsupported.includes(node.type.name);
|
|
39
|
+
};
|
|
40
|
+
return Boolean($cursor === null || $cursor === void 0 ? void 0 : (_$cursor$nodeBefore = $cursor.nodeBefore) === null || _$cursor$nodeBefore === void 0 ? void 0 : (_$cursor$nodeBefore$m = _$cursor$nodeBefore.marks) === null || _$cursor$nodeBefore$m === void 0 ? void 0 : _$cursor$nodeBefore$m.some(isUnsupportedMark));
|
|
41
|
+
};
|
|
42
|
+
var createPlugin = function createPlugin(pluginName, rules) {
|
|
43
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
44
|
+
var _options$isBlockNodeR = options.isBlockNodeRule,
|
|
45
|
+
isBlockNodeRule = _options$isBlockNodeR === void 0 ? false : _options$isBlockNodeR,
|
|
46
|
+
_options$allowInsertT = options.allowInsertTextOnDocument,
|
|
47
|
+
allowInsertTextOnDocument = _options$allowInsertT === void 0 ? true : _options$allowInsertT;
|
|
48
|
+
var onInputEvent = function onInputEvent(_ref) {
|
|
49
|
+
var state = _ref.state,
|
|
50
|
+
from = _ref.from,
|
|
51
|
+
to = _ref.to;
|
|
52
|
+
var unsupportedMarks = isBlockNodeRule ? ['code', 'link', 'typeAheadQuery'] : ['code'];
|
|
53
|
+
var $from = state.selection.$from;
|
|
54
|
+
if ($from.parent.type.spec.code || !(state.selection instanceof _prosemirrorState.TextSelection) && !(state.selection instanceof _selection.GapCursorSelection) || hasUnsupportedMarks(state, from, to, unsupportedMarks) || isBlockNodeRule && isCursorInsideUnsupportedMarks(state, unsupportedMarks)) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
};
|
|
59
|
+
return (0, _plugin.createInputRulePlugin)(pluginName, rules, {
|
|
60
|
+
allowInsertTextOnDocument: allowInsertTextOnDocument,
|
|
61
|
+
onInputEvent: onInputEvent,
|
|
62
|
+
onBeforeRegexMatch: function onBeforeRegexMatch(tr) {
|
|
63
|
+
(0, _prosemirrorHistory.closeHistory)(tr);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
exports.createPlugin = createPlugin;
|
package/dist/cjs/version.json
CHANGED
package/dist/es2019/index.js
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { closeHistory } from 'prosemirror-history';
|
|
2
|
+
import { TextSelection } from 'prosemirror-state';
|
|
3
|
+
import { GapCursorSelection } from '@atlaskit/editor-common/selection';
|
|
4
|
+
import { createInputRulePlugin } from './plugin';
|
|
5
|
+
export const createRule = (match, handler) => {
|
|
6
|
+
return {
|
|
7
|
+
match,
|
|
8
|
+
handler,
|
|
9
|
+
onHandlerApply: (state, tr) => {
|
|
10
|
+
closeHistory(tr);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
const hasUnsupportedMarks = (state, start, end, marksNameUnsupported) => {
|
|
15
|
+
const isUnsupportedMark = node => (marksNameUnsupported || []).includes(node.type.name);
|
|
16
|
+
const $from = state.doc.resolve(start);
|
|
17
|
+
const $to = state.doc.resolve(end);
|
|
18
|
+
const marksInSelection = start === end ? $from.marks() : $from.marksAcross($to);
|
|
19
|
+
return (marksInSelection || []).some(isUnsupportedMark);
|
|
20
|
+
};
|
|
21
|
+
const isCursorInsideUnsupportedMarks = (state, marksNameUnsupported) => {
|
|
22
|
+
var _$cursor$nodeBefore, _$cursor$nodeBefore$m;
|
|
23
|
+
const {
|
|
24
|
+
selection
|
|
25
|
+
} = state;
|
|
26
|
+
if (!(selection instanceof TextSelection)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
const {
|
|
30
|
+
$cursor
|
|
31
|
+
} = selection;
|
|
32
|
+
const isUnsupportedMark = node => marksNameUnsupported.includes(node.type.name);
|
|
33
|
+
return Boolean($cursor === null || $cursor === void 0 ? void 0 : (_$cursor$nodeBefore = $cursor.nodeBefore) === null || _$cursor$nodeBefore === void 0 ? void 0 : (_$cursor$nodeBefore$m = _$cursor$nodeBefore.marks) === null || _$cursor$nodeBefore$m === void 0 ? void 0 : _$cursor$nodeBefore$m.some(isUnsupportedMark));
|
|
34
|
+
};
|
|
35
|
+
export const createPlugin = (pluginName, rules, options = {}) => {
|
|
36
|
+
const {
|
|
37
|
+
isBlockNodeRule = false,
|
|
38
|
+
allowInsertTextOnDocument = true
|
|
39
|
+
} = options;
|
|
40
|
+
const onInputEvent = ({
|
|
41
|
+
state,
|
|
42
|
+
from,
|
|
43
|
+
to
|
|
44
|
+
}) => {
|
|
45
|
+
const unsupportedMarks = isBlockNodeRule ? ['code', 'link', 'typeAheadQuery'] : ['code'];
|
|
46
|
+
const $from = state.selection.$from;
|
|
47
|
+
if ($from.parent.type.spec.code || !(state.selection instanceof TextSelection) && !(state.selection instanceof GapCursorSelection) || hasUnsupportedMarks(state, from, to, unsupportedMarks) || isBlockNodeRule && isCursorInsideUnsupportedMarks(state, unsupportedMarks)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
return createInputRulePlugin(pluginName, rules, {
|
|
53
|
+
allowInsertTextOnDocument,
|
|
54
|
+
onInputEvent,
|
|
55
|
+
onBeforeRegexMatch: tr => {
|
|
56
|
+
closeHistory(tr);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
};
|
package/dist/es2019/version.json
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { closeHistory } from 'prosemirror-history';
|
|
2
|
+
import { TextSelection } from 'prosemirror-state';
|
|
3
|
+
import { GapCursorSelection } from '@atlaskit/editor-common/selection';
|
|
4
|
+
import { createInputRulePlugin } from './plugin';
|
|
5
|
+
export var createRule = function createRule(match, handler) {
|
|
6
|
+
return {
|
|
7
|
+
match: match,
|
|
8
|
+
handler: handler,
|
|
9
|
+
onHandlerApply: function onHandlerApply(state, tr) {
|
|
10
|
+
closeHistory(tr);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
var hasUnsupportedMarks = function hasUnsupportedMarks(state, start, end, marksNameUnsupported) {
|
|
15
|
+
var isUnsupportedMark = function isUnsupportedMark(node) {
|
|
16
|
+
return (marksNameUnsupported || []).includes(node.type.name);
|
|
17
|
+
};
|
|
18
|
+
var $from = state.doc.resolve(start);
|
|
19
|
+
var $to = state.doc.resolve(end);
|
|
20
|
+
var marksInSelection = start === end ? $from.marks() : $from.marksAcross($to);
|
|
21
|
+
return (marksInSelection || []).some(isUnsupportedMark);
|
|
22
|
+
};
|
|
23
|
+
var isCursorInsideUnsupportedMarks = function isCursorInsideUnsupportedMarks(state, marksNameUnsupported) {
|
|
24
|
+
var _$cursor$nodeBefore, _$cursor$nodeBefore$m;
|
|
25
|
+
var selection = state.selection;
|
|
26
|
+
if (!(selection instanceof TextSelection)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
var $cursor = selection.$cursor;
|
|
30
|
+
var isUnsupportedMark = function isUnsupportedMark(node) {
|
|
31
|
+
return marksNameUnsupported.includes(node.type.name);
|
|
32
|
+
};
|
|
33
|
+
return Boolean($cursor === null || $cursor === void 0 ? void 0 : (_$cursor$nodeBefore = $cursor.nodeBefore) === null || _$cursor$nodeBefore === void 0 ? void 0 : (_$cursor$nodeBefore$m = _$cursor$nodeBefore.marks) === null || _$cursor$nodeBefore$m === void 0 ? void 0 : _$cursor$nodeBefore$m.some(isUnsupportedMark));
|
|
34
|
+
};
|
|
35
|
+
export var createPlugin = function createPlugin(pluginName, rules) {
|
|
36
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
37
|
+
var _options$isBlockNodeR = options.isBlockNodeRule,
|
|
38
|
+
isBlockNodeRule = _options$isBlockNodeR === void 0 ? false : _options$isBlockNodeR,
|
|
39
|
+
_options$allowInsertT = options.allowInsertTextOnDocument,
|
|
40
|
+
allowInsertTextOnDocument = _options$allowInsertT === void 0 ? true : _options$allowInsertT;
|
|
41
|
+
var onInputEvent = function onInputEvent(_ref) {
|
|
42
|
+
var state = _ref.state,
|
|
43
|
+
from = _ref.from,
|
|
44
|
+
to = _ref.to;
|
|
45
|
+
var unsupportedMarks = isBlockNodeRule ? ['code', 'link', 'typeAheadQuery'] : ['code'];
|
|
46
|
+
var $from = state.selection.$from;
|
|
47
|
+
if ($from.parent.type.spec.code || !(state.selection instanceof TextSelection) && !(state.selection instanceof GapCursorSelection) || hasUnsupportedMarks(state, from, to, unsupportedMarks) || isBlockNodeRule && isCursorInsideUnsupportedMarks(state, unsupportedMarks)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
return createInputRulePlugin(pluginName, rules, {
|
|
53
|
+
allowInsertTextOnDocument: allowInsertTextOnDocument,
|
|
54
|
+
onInputEvent: onInputEvent,
|
|
55
|
+
onBeforeRegexMatch: function onBeforeRegexMatch(tr) {
|
|
56
|
+
closeHistory(tr);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
};
|
package/dist/esm/version.json
CHANGED
package/dist/types/index.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ import type { InputRuleHandler, InputRuleWrapper, OnHandlerApply, OnInputEvent }
|
|
|
2
2
|
export { leafNodeReplacementCharacter, TEXT_INPUT_RULE_TRANSACTION_KEY, MAX_REGEX_MATCH, } from './constants';
|
|
3
3
|
export { createInputRulePlugin } from './plugin';
|
|
4
4
|
export type { InputRuleWrapper, InputRuleHandler, OnInputEvent, OnHandlerApply, };
|
|
5
|
+
export { createRule, createPlugin } from './utils';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
|
|
2
|
+
import type { InputRuleHandler, InputRuleWrapper } from './types';
|
|
3
|
+
export declare const createRule: (match: RegExp, handler: InputRuleHandler) => InputRuleWrapper;
|
|
4
|
+
type Options = {
|
|
5
|
+
isBlockNodeRule?: boolean;
|
|
6
|
+
allowInsertTextOnDocument?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const createPlugin: (pluginName: string, rules: Array<InputRuleWrapper>, options?: Options) => SafePlugin;
|
|
9
|
+
export {};
|
|
@@ -2,3 +2,4 @@ import type { InputRuleHandler, InputRuleWrapper, OnHandlerApply, OnInputEvent }
|
|
|
2
2
|
export { leafNodeReplacementCharacter, TEXT_INPUT_RULE_TRANSACTION_KEY, MAX_REGEX_MATCH, } from './constants';
|
|
3
3
|
export { createInputRulePlugin } from './plugin';
|
|
4
4
|
export type { InputRuleWrapper, InputRuleHandler, OnInputEvent, OnHandlerApply, };
|
|
5
|
+
export { createRule, createPlugin } from './utils';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
|
|
2
|
+
import type { InputRuleHandler, InputRuleWrapper } from './types';
|
|
3
|
+
export declare const createRule: (match: RegExp, handler: InputRuleHandler) => InputRuleWrapper;
|
|
4
|
+
type Options = {
|
|
5
|
+
isBlockNodeRule?: boolean;
|
|
6
|
+
allowInsertTextOnDocument?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const createPlugin: (pluginName: string, rules: Array<InputRuleWrapper>, options?: Options) => SafePlugin;
|
|
9
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaskit/prosemirror-input-rules",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "A package that contains helpers to create autoformatting rules for ProseMirror",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -28,17 +28,15 @@
|
|
|
28
28
|
"releaseModel": "continuous"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@atlaskit/editor-common": "^74.
|
|
31
|
+
"@atlaskit/editor-common": "^74.20.0",
|
|
32
32
|
"@babel/runtime": "^7.0.0",
|
|
33
|
+
"prosemirror-history": "^1.1.3",
|
|
34
|
+
"prosemirror-model": "1.16.0",
|
|
33
35
|
"prosemirror-state": "1.3.4"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
|
-
"@atlaskit/editor-test-helpers": "^18.
|
|
38
|
+
"@atlaskit/editor-test-helpers": "^18.10.0",
|
|
37
39
|
"@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
|
|
38
|
-
"@types/prosemirror-history": "^1.0.1",
|
|
39
|
-
"@types/prosemirror-model": "^1.11.0",
|
|
40
|
-
"@types/prosemirror-state": "^1.2.0",
|
|
41
|
-
"@types/prosemirror-view": "^1.9.0",
|
|
42
40
|
"prosemirror-view": "1.23.7"
|
|
43
41
|
},
|
|
44
42
|
"techstack": {
|
package/report.api.md
CHANGED
|
@@ -26,6 +26,19 @@ export function createInputRulePlugin(
|
|
|
26
26
|
options?: Options,
|
|
27
27
|
): SafePlugin;
|
|
28
28
|
|
|
29
|
+
// @public (undocumented)
|
|
30
|
+
export const createPlugin: (
|
|
31
|
+
pluginName: string,
|
|
32
|
+
rules: Array<InputRuleWrapper>,
|
|
33
|
+
options?: Options_2,
|
|
34
|
+
) => SafePlugin;
|
|
35
|
+
|
|
36
|
+
// @public (undocumented)
|
|
37
|
+
export const createRule: (
|
|
38
|
+
match: RegExp,
|
|
39
|
+
handler: InputRuleHandler,
|
|
40
|
+
) => InputRuleWrapper;
|
|
41
|
+
|
|
29
42
|
// @public (undocumented)
|
|
30
43
|
export type InputRuleHandler = (
|
|
31
44
|
state: EditorState,
|
|
@@ -74,6 +87,12 @@ type Options = {
|
|
|
74
87
|
onBeforeRegexMatch?: OnBeforeRegexMatch;
|
|
75
88
|
};
|
|
76
89
|
|
|
90
|
+
// @public (undocumented)
|
|
91
|
+
type Options_2 = {
|
|
92
|
+
isBlockNodeRule?: boolean;
|
|
93
|
+
allowInsertTextOnDocument?: boolean;
|
|
94
|
+
};
|
|
95
|
+
|
|
77
96
|
// @public (undocumented)
|
|
78
97
|
export const TEXT_INPUT_RULE_TRANSACTION_KEY = 'input_rule_plugin_transaction';
|
|
79
98
|
|
|
@@ -27,7 +27,7 @@ describe('input-tule/handles/createInputEventHandler', () => {
|
|
|
27
27
|
|
|
28
28
|
beforeEach(() => {
|
|
29
29
|
initialEditorState = createEditorState(doc(p('before{<>}')));
|
|
30
|
-
editorView = new EditorView(
|
|
30
|
+
editorView = new EditorView(null, {
|
|
31
31
|
state: initialEditorState,
|
|
32
32
|
});
|
|
33
33
|
|
|
@@ -52,7 +52,7 @@ describe('input-tule/handles/createInputEventHandler', () => {
|
|
|
52
52
|
let inputHandleResult: boolean;
|
|
53
53
|
beforeEach(() => {
|
|
54
54
|
const newEditorState = createEditorState(doc(code_block()('before{<>}')));
|
|
55
|
-
editorView = new EditorView(
|
|
55
|
+
editorView = new EditorView(null, {
|
|
56
56
|
state: newEditorState,
|
|
57
57
|
});
|
|
58
58
|
|
package/src/index.ts
CHANGED
package/src/utils.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { closeHistory } from 'prosemirror-history';
|
|
2
|
+
import { Mark as PMMark } from 'prosemirror-model';
|
|
3
|
+
import { EditorState, TextSelection } from 'prosemirror-state';
|
|
4
|
+
|
|
5
|
+
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
|
|
6
|
+
import { GapCursorSelection } from '@atlaskit/editor-common/selection';
|
|
7
|
+
|
|
8
|
+
import { createInputRulePlugin } from './plugin';
|
|
9
|
+
import type { InputRuleHandler, InputRuleWrapper, OnInputEvent } from './types';
|
|
10
|
+
|
|
11
|
+
export const createRule = (
|
|
12
|
+
match: RegExp,
|
|
13
|
+
handler: InputRuleHandler,
|
|
14
|
+
): InputRuleWrapper => {
|
|
15
|
+
return {
|
|
16
|
+
match,
|
|
17
|
+
handler,
|
|
18
|
+
onHandlerApply: (state, tr) => {
|
|
19
|
+
closeHistory(tr);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const hasUnsupportedMarks = (
|
|
25
|
+
state: EditorState,
|
|
26
|
+
start: number,
|
|
27
|
+
end: number,
|
|
28
|
+
marksNameUnsupported: string[],
|
|
29
|
+
) => {
|
|
30
|
+
const isUnsupportedMark = (node: PMMark) =>
|
|
31
|
+
(marksNameUnsupported || []).includes(node.type.name);
|
|
32
|
+
|
|
33
|
+
const $from = state.doc.resolve(start);
|
|
34
|
+
const $to = state.doc.resolve(end);
|
|
35
|
+
const marksInSelection =
|
|
36
|
+
start === end ? $from.marks() : $from.marksAcross($to);
|
|
37
|
+
|
|
38
|
+
return (marksInSelection || []).some(isUnsupportedMark);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const isCursorInsideUnsupportedMarks = (
|
|
42
|
+
state: EditorState,
|
|
43
|
+
marksNameUnsupported: string[],
|
|
44
|
+
): boolean => {
|
|
45
|
+
const { selection } = state;
|
|
46
|
+
if (!(selection instanceof TextSelection)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
const { $cursor } = selection;
|
|
50
|
+
const isUnsupportedMark = (node: PMMark) =>
|
|
51
|
+
marksNameUnsupported.includes(node.type.name);
|
|
52
|
+
|
|
53
|
+
return Boolean($cursor?.nodeBefore?.marks?.some(isUnsupportedMark));
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
type Options = {
|
|
57
|
+
isBlockNodeRule?: boolean;
|
|
58
|
+
allowInsertTextOnDocument?: boolean;
|
|
59
|
+
};
|
|
60
|
+
export const createPlugin = (
|
|
61
|
+
pluginName: string,
|
|
62
|
+
rules: Array<InputRuleWrapper>,
|
|
63
|
+
options: Options = {},
|
|
64
|
+
): SafePlugin => {
|
|
65
|
+
const { isBlockNodeRule = false, allowInsertTextOnDocument = true } = options;
|
|
66
|
+
|
|
67
|
+
const onInputEvent: OnInputEvent = ({ state, from, to }) => {
|
|
68
|
+
const unsupportedMarks = isBlockNodeRule
|
|
69
|
+
? ['code', 'link', 'typeAheadQuery']
|
|
70
|
+
: ['code'];
|
|
71
|
+
|
|
72
|
+
const $from = state.selection.$from;
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
$from.parent.type.spec.code ||
|
|
76
|
+
(!(state.selection instanceof TextSelection) &&
|
|
77
|
+
!(state.selection instanceof GapCursorSelection)) ||
|
|
78
|
+
hasUnsupportedMarks(state, from, to, unsupportedMarks) ||
|
|
79
|
+
(isBlockNodeRule &&
|
|
80
|
+
isCursorInsideUnsupportedMarks(state, unsupportedMarks))
|
|
81
|
+
) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return true;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return createInputRulePlugin(pluginName, rules, {
|
|
89
|
+
allowInsertTextOnDocument,
|
|
90
|
+
onInputEvent,
|
|
91
|
+
onBeforeRegexMatch: (tr) => {
|
|
92
|
+
closeHistory(tr);
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
};
|
package/tmp/api-report-tmp.d.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
## API Report File for "@atlaskit/prosemirror-input-rules"
|
|
2
|
-
|
|
3
|
-
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
|
|
4
|
-
|
|
5
|
-
```ts
|
|
6
|
-
|
|
7
|
-
import type { EditorState } from 'prosemirror-state';
|
|
8
|
-
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
|
|
9
|
-
import type { Transaction } from 'prosemirror-state';
|
|
10
|
-
|
|
11
|
-
// @public (undocumented)
|
|
12
|
-
export function createInputRulePlugin(pluginName: string, rules: InputRuleWrapper[], options?: Options): SafePlugin;
|
|
13
|
-
|
|
14
|
-
// @public (undocumented)
|
|
15
|
-
export type InputRuleHandler = (state: EditorState, matchResult: RegExpExecArray, start: number, end: number) => Transaction | null;
|
|
16
|
-
|
|
17
|
-
// @public (undocumented)
|
|
18
|
-
export interface InputRuleWrapper {
|
|
19
|
-
// (undocumented)
|
|
20
|
-
handler: InputRuleHandler;
|
|
21
|
-
// (undocumented)
|
|
22
|
-
match: RegExp;
|
|
23
|
-
// (undocumented)
|
|
24
|
-
onHandlerApply?: OnHandlerApply;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// @public (undocumented)
|
|
28
|
-
export const leafNodeReplacementCharacter = "\uFFFC";
|
|
29
|
-
|
|
30
|
-
// @public (undocumented)
|
|
31
|
-
export const MAX_REGEX_MATCH = 500;
|
|
32
|
-
|
|
33
|
-
// @public (undocumented)
|
|
34
|
-
type OnBeforeRegexMatch = (tr: Transaction) => void;
|
|
35
|
-
|
|
36
|
-
// @public (undocumented)
|
|
37
|
-
export type OnHandlerApply = (state: EditorState, tr: Transaction, matchResult: RegExpExecArray) => void;
|
|
38
|
-
|
|
39
|
-
// @public (undocumented)
|
|
40
|
-
export type OnInputEvent = (props: {
|
|
41
|
-
state: EditorState;
|
|
42
|
-
from: number;
|
|
43
|
-
to: number;
|
|
44
|
-
}) => boolean;
|
|
45
|
-
|
|
46
|
-
// @public (undocumented)
|
|
47
|
-
type Options = {
|
|
48
|
-
allowInsertTextOnDocument?: boolean;
|
|
49
|
-
onInputEvent?: OnInputEvent;
|
|
50
|
-
onBeforeRegexMatch?: OnBeforeRegexMatch;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// @public (undocumented)
|
|
54
|
-
export const TEXT_INPUT_RULE_TRANSACTION_KEY = "input_rule_plugin_transaction";
|
|
55
|
-
|
|
56
|
-
// (No @packageDocumentation comment for this package)
|
|
57
|
-
|
|
58
|
-
```
|