@portabletext/plugin-input-rule 0.3.2 → 0.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/README.md +22 -18
- package/package.json +3 -3
- package/src/rule.markdown-link.feature +0 -54
- package/src/rule.markdown-link.test.tsx +0 -46
- package/src/rule.markdown-link.ts +0 -98
package/README.md
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
> Easily configure Input Rules in the Portable Text Editor
|
|
4
4
|
|
|
5
|
-
> ⚠️
|
|
5
|
+
> ⚠️ **Note:** `defineInputRule` and other APIs exposed by this plugin are still a work in progress and may change slightly. We are still ironing out some details before we can cut a stable release.
|
|
6
6
|
|
|
7
|
-
Listening for
|
|
7
|
+
Listening for inserted text patterns and performing actions based on them is incredibly common, but it comes with many challenges:
|
|
8
8
|
|
|
9
9
|
1. How do you implement undo functionality correctly?
|
|
10
10
|
2. What about smart undo with <kbd>Backspace</kbd>?
|
|
11
|
-
3.
|
|
11
|
+
3. Have you considered `insert.text` events that carry more than one character? (Android, anyone?)
|
|
12
12
|
|
|
13
|
-
_This is why this plugin exists_. It brings the concept of "Input Rules" to the Portable Text Editor
|
|
13
|
+
_This is why this plugin exists_. It brings the concept of "Input Rules" to the Portable Text Editor, allowing you to write text transformation logic as if they were Behaviors, without worrying about low-level details:
|
|
14
14
|
|
|
15
15
|
```tsx
|
|
16
16
|
import type {EditorSchema} from '@portabletext/editor'
|
|
@@ -19,13 +19,13 @@ import {getPreviousInlineObject} from '@portabletext/editor/selectors'
|
|
|
19
19
|
import {defineInputRule} from '@portabletext/plugin-input-rule'
|
|
20
20
|
|
|
21
21
|
const unorderedListRule = defineInputRule({
|
|
22
|
-
//
|
|
22
|
+
// Listen for a RegExp pattern instead of a raw event
|
|
23
23
|
on: /^(-|\*) /,
|
|
24
24
|
// The `event` carries useful information like the offsets of RegExp matches
|
|
25
|
-
// as well as information about the focused text block
|
|
25
|
+
// as well as information about the focused text block
|
|
26
26
|
guard: ({snapshot, event}) => {
|
|
27
|
-
// In theory, an Input Rule could return multiple matches
|
|
28
|
-
// case we only expect one
|
|
27
|
+
// In theory, an Input Rule could return multiple matches, but in this
|
|
28
|
+
// case we only expect one
|
|
29
29
|
const match = event.matches.at(0)
|
|
30
30
|
|
|
31
31
|
if (!match) {
|
|
@@ -42,7 +42,7 @@ const unorderedListRule = defineInputRule({
|
|
|
42
42
|
props: ['style'],
|
|
43
43
|
at: event.focusTextBlock.path,
|
|
44
44
|
}),
|
|
45
|
-
// Then, turn it into a list
|
|
45
|
+
// Then, turn it into a list item
|
|
46
46
|
raise({
|
|
47
47
|
type: 'block.set',
|
|
48
48
|
props: {
|
|
@@ -65,11 +65,11 @@ export function MyMarkdownPlugin() {
|
|
|
65
65
|
}
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
> 💡 **Tip:** The [`@portabletext/plugin-markdown-shortcuts`](../plugin-markdown-shortcuts/) package is already built using Input Rules and provides common markdown shortcuts out of the box.
|
|
69
69
|
|
|
70
70
|
## Text Transformation Rules
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
Text transformations are so common that the plugin provides a high-level `defineTextTransformRule` helper to configure them without any boilerplate:
|
|
73
73
|
|
|
74
74
|
```tsx
|
|
75
75
|
const emDashRule = defineTextTransformRule({
|
|
@@ -78,20 +78,22 @@ const emDashRule = defineTextTransformRule({
|
|
|
78
78
|
})
|
|
79
79
|
|
|
80
80
|
export function MyTypographyPlugin() {
|
|
81
|
-
return <InputRulePlugin rules={[
|
|
81
|
+
return <InputRulePlugin rules={[emDashRule]} />
|
|
82
82
|
}
|
|
83
83
|
```
|
|
84
84
|
|
|
85
85
|
In fact, the production-ready [`@portabletext/plugin-typography`](../plugin-typography/) is built on top of Input Rules and comes packed with common text transformations like this.
|
|
86
86
|
|
|
87
|
-
##
|
|
87
|
+
## Advanced Examples
|
|
88
88
|
|
|
89
|
-
|
|
89
|
+
Input Rules can handle more complex transformations. Here are two advanced examples:
|
|
90
90
|
|
|
91
|
-
1. Automatically
|
|
92
|
-
2.
|
|
91
|
+
1. **Markdown Link**: Automatically convert `[text](url)` syntax into proper links
|
|
92
|
+
2. **Stock Ticker**: Convert `{SYMBOL}` patterns into stock ticker objects
|
|
93
93
|
|
|
94
|
-
### Markdown Link
|
|
94
|
+
### Markdown Link Rule
|
|
95
|
+
|
|
96
|
+
This example shows how to convert markdown-style link syntax `[text](url)` into proper link annotations:
|
|
95
97
|
|
|
96
98
|
```tsx
|
|
97
99
|
const markdownLinkRule = defineInputRule({
|
|
@@ -176,7 +178,9 @@ const markdownLinkRule = defineInputRule({
|
|
|
176
178
|
})
|
|
177
179
|
```
|
|
178
180
|
|
|
179
|
-
|
|
181
|
+
### Stock Ticker Rule
|
|
182
|
+
|
|
183
|
+
This example demonstrates how to convert text patterns like `{AAPL}` into custom inline objects:
|
|
180
184
|
|
|
181
185
|
```tsx
|
|
182
186
|
const stockTickerRule = defineInputRule({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portabletext/plugin-input-rule",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "Easily configure input rules in the Portable Text Editor",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"portabletext",
|
|
@@ -53,12 +53,12 @@
|
|
|
53
53
|
"typescript": "5.9.3",
|
|
54
54
|
"typescript-eslint": "^8.41.0",
|
|
55
55
|
"vitest": "^3.2.4",
|
|
56
|
-
"@portabletext/editor": "2.14.
|
|
56
|
+
"@portabletext/editor": "2.14.3",
|
|
57
57
|
"@portabletext/schema": "1.2.0",
|
|
58
58
|
"racejar": "1.3.1"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
|
-
"@portabletext/editor": "^2.14.
|
|
61
|
+
"@portabletext/editor": "^2.14.3",
|
|
62
62
|
"react": "^19.1.1"
|
|
63
63
|
},
|
|
64
64
|
"publishConfig": {
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
Feature: Markdown Link Rule
|
|
2
|
-
|
|
3
|
-
Background:
|
|
4
|
-
Given the editor is focused
|
|
5
|
-
And a global keymap
|
|
6
|
-
|
|
7
|
-
Scenario Outline: Transform markdown Link into annotation
|
|
8
|
-
Given the text <text>
|
|
9
|
-
When <inserted text> is inserted
|
|
10
|
-
And "new" is typed
|
|
11
|
-
Then the text is <new text>
|
|
12
|
-
And <annotated> has marks "k4"
|
|
13
|
-
|
|
14
|
-
Examples:
|
|
15
|
-
| text | inserted text | new text | annotated |
|
|
16
|
-
| "[foo](bar" | ")" | "foo,new" | "foo" |
|
|
17
|
-
|
|
18
|
-
Scenario: Preserving decorator in link text
|
|
19
|
-
Given the text "[foo](bar"
|
|
20
|
-
And "strong" around "foo"
|
|
21
|
-
When ")" is inserted
|
|
22
|
-
And "new" is typed
|
|
23
|
-
Then the text is "foo,new"
|
|
24
|
-
And "foo" has marks "strong,k6"
|
|
25
|
-
|
|
26
|
-
Scenario: Preserving decorators in link text
|
|
27
|
-
Given the text "[foo](bar"
|
|
28
|
-
And "strong" around "foo"
|
|
29
|
-
And "em" around "oo"
|
|
30
|
-
When ")" is inserted
|
|
31
|
-
And "new" is typed
|
|
32
|
-
Then the text is "f,oo,new"
|
|
33
|
-
And "f" has marks "strong,k7"
|
|
34
|
-
And "oo" has marks "strong,em,k7"
|
|
35
|
-
|
|
36
|
-
Scenario: Overwriting other links
|
|
37
|
-
Given the text "[foo](bar"
|
|
38
|
-
And a "link" "l1" around "foo"
|
|
39
|
-
When the caret is put after "bar"
|
|
40
|
-
And ")" is inserted
|
|
41
|
-
And "new" is typed
|
|
42
|
-
Then the text is "foo,new"
|
|
43
|
-
And "foo" has an annotation different than "l1"
|
|
44
|
-
|
|
45
|
-
Scenario: Preserving other annotations
|
|
46
|
-
Given the text "[foo](bar"
|
|
47
|
-
And a "link" "l1" around "foo"
|
|
48
|
-
And a "comment" "c1" around "foo"
|
|
49
|
-
When the caret is put after "bar"
|
|
50
|
-
And ")" is inserted
|
|
51
|
-
And "new" is typed
|
|
52
|
-
Then the text is "foo,new"
|
|
53
|
-
And "foo" has an annotation different than "l1"
|
|
54
|
-
And "foo" has marks "c1,k9"
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import {parameterTypes} from '@portabletext/editor/test'
|
|
2
|
-
import {
|
|
3
|
-
createTestEditor,
|
|
4
|
-
stepDefinitions,
|
|
5
|
-
type Context,
|
|
6
|
-
} from '@portabletext/editor/test/vitest'
|
|
7
|
-
import {defineSchema} from '@portabletext/schema'
|
|
8
|
-
import {Before} from 'racejar'
|
|
9
|
-
import {Feature} from 'racejar/vitest'
|
|
10
|
-
import {InputRulePlugin} from './plugin.input-rule'
|
|
11
|
-
import {createMarkdownLinkRule} from './rule.markdown-link'
|
|
12
|
-
import markdownLinkFeature from './rule.markdown-link.feature?raw'
|
|
13
|
-
|
|
14
|
-
const markdownLinkRule = createMarkdownLinkRule({
|
|
15
|
-
linkObject: (context) => ({
|
|
16
|
-
name: 'link',
|
|
17
|
-
value: {
|
|
18
|
-
href: context.href,
|
|
19
|
-
},
|
|
20
|
-
}),
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
Feature({
|
|
24
|
-
hooks: [
|
|
25
|
-
Before(async (context: Context) => {
|
|
26
|
-
const {editor, locator} = await createTestEditor({
|
|
27
|
-
children: (
|
|
28
|
-
<>
|
|
29
|
-
<InputRulePlugin rules={[markdownLinkRule]} />
|
|
30
|
-
</>
|
|
31
|
-
),
|
|
32
|
-
schemaDefinition: defineSchema({
|
|
33
|
-
decorators: [{name: 'strong'}, {name: 'em'}],
|
|
34
|
-
annotations: [{name: 'link'}, {name: 'comment'}],
|
|
35
|
-
inlineObjects: [{name: 'stock-ticker'}],
|
|
36
|
-
}),
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
context.locator = locator
|
|
40
|
-
context.editor = editor
|
|
41
|
-
}),
|
|
42
|
-
],
|
|
43
|
-
featureText: markdownLinkFeature,
|
|
44
|
-
stepDefinitions,
|
|
45
|
-
parameterTypes,
|
|
46
|
-
})
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import type {EditorSchema} from '@portabletext/editor'
|
|
2
|
-
import {raise, type BehaviorAction} from '@portabletext/editor/behaviors'
|
|
3
|
-
import {defineInputRule} from './input-rule'
|
|
4
|
-
|
|
5
|
-
export function createMarkdownLinkRule(config: {
|
|
6
|
-
linkObject: (context: {
|
|
7
|
-
schema: EditorSchema
|
|
8
|
-
href: string
|
|
9
|
-
}) => {name: string; value?: {[prop: string]: unknown}} | undefined
|
|
10
|
-
}) {
|
|
11
|
-
return defineInputRule({
|
|
12
|
-
on: /\[(.+)]\((.+)\)/,
|
|
13
|
-
actions: [
|
|
14
|
-
({snapshot, event}) => {
|
|
15
|
-
const newText = event.textBefore + event.textInserted
|
|
16
|
-
let textLengthDelta = 0
|
|
17
|
-
const actions: Array<BehaviorAction> = []
|
|
18
|
-
|
|
19
|
-
for (const match of event.matches.reverse()) {
|
|
20
|
-
const textMatch = match.groupMatches.at(0)
|
|
21
|
-
const hrefMatch = match.groupMatches.at(1)
|
|
22
|
-
|
|
23
|
-
if (textMatch === undefined || hrefMatch === undefined) {
|
|
24
|
-
continue
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
textLengthDelta =
|
|
28
|
-
textLengthDelta -
|
|
29
|
-
(match.targetOffsets.focus.offset -
|
|
30
|
-
match.targetOffsets.anchor.offset -
|
|
31
|
-
textMatch.text.length)
|
|
32
|
-
|
|
33
|
-
const linkObject = config.linkObject({
|
|
34
|
-
schema: snapshot.context.schema,
|
|
35
|
-
href: hrefMatch.text,
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
if (!linkObject) {
|
|
39
|
-
continue
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const leftSideOffsets = {
|
|
43
|
-
anchor: match.targetOffsets.anchor,
|
|
44
|
-
focus: textMatch.targetOffsets.anchor,
|
|
45
|
-
}
|
|
46
|
-
const rightSideOffsets = {
|
|
47
|
-
anchor: textMatch.targetOffsets.focus,
|
|
48
|
-
focus: match.targetOffsets.focus,
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
actions.push(
|
|
52
|
-
raise({
|
|
53
|
-
type: 'select',
|
|
54
|
-
at: textMatch.targetOffsets,
|
|
55
|
-
}),
|
|
56
|
-
)
|
|
57
|
-
actions.push(
|
|
58
|
-
raise({
|
|
59
|
-
type: 'annotation.add',
|
|
60
|
-
annotation: {
|
|
61
|
-
name: linkObject.name,
|
|
62
|
-
value: linkObject.value ?? {},
|
|
63
|
-
},
|
|
64
|
-
}),
|
|
65
|
-
)
|
|
66
|
-
actions.push(
|
|
67
|
-
raise({
|
|
68
|
-
type: 'delete',
|
|
69
|
-
at: rightSideOffsets,
|
|
70
|
-
}),
|
|
71
|
-
)
|
|
72
|
-
actions.push(
|
|
73
|
-
raise({
|
|
74
|
-
type: 'delete',
|
|
75
|
-
at: leftSideOffsets,
|
|
76
|
-
}),
|
|
77
|
-
)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const endCaretPosition = {
|
|
81
|
-
path: event.focusTextBlock.path,
|
|
82
|
-
offset: newText.length - textLengthDelta * -1,
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return [
|
|
86
|
-
...actions,
|
|
87
|
-
raise({
|
|
88
|
-
type: 'select',
|
|
89
|
-
at: {
|
|
90
|
-
anchor: endCaretPosition,
|
|
91
|
-
focus: endCaretPosition,
|
|
92
|
-
},
|
|
93
|
-
}),
|
|
94
|
-
]
|
|
95
|
-
},
|
|
96
|
-
],
|
|
97
|
-
})
|
|
98
|
-
}
|