@portabletext/plugin-markdown-shortcuts 1.2.0 → 1.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.
@@ -0,0 +1,59 @@
1
+ import type {EditorSchema} from '@portabletext/editor'
2
+ import {raise} from '@portabletext/editor/behaviors'
3
+ import {getPreviousInlineObject} from '@portabletext/editor/selectors'
4
+ import {defineInputRule} from '@portabletext/plugin-input-rule'
5
+
6
+ export function createHeadingRule(config: {
7
+ headingStyle: (context: {
8
+ schema: EditorSchema
9
+ level: number
10
+ }) => string | undefined
11
+ }) {
12
+ return defineInputRule({
13
+ on: /^#+ /,
14
+ guard: ({snapshot, event}) => {
15
+ const previousInlineObject = getPreviousInlineObject(snapshot)
16
+
17
+ if (previousInlineObject) {
18
+ return false
19
+ }
20
+
21
+ const match = event.matches.at(0)
22
+
23
+ if (!match) {
24
+ return false
25
+ }
26
+
27
+ const level = match.text.length - 1
28
+
29
+ const style = config.headingStyle({
30
+ schema: snapshot.context.schema,
31
+ level,
32
+ })
33
+
34
+ if (!style) {
35
+ return false
36
+ }
37
+
38
+ return {match, style}
39
+ },
40
+ actions: [
41
+ ({event}, {match, style}) => [
42
+ raise({
43
+ type: 'block.unset',
44
+ props: ['listItem', 'level'],
45
+ at: event.focusTextBlock.path,
46
+ }),
47
+ raise({
48
+ type: 'block.set',
49
+ props: {style},
50
+ at: event.focusTextBlock.path,
51
+ }),
52
+ raise({
53
+ type: 'delete',
54
+ at: match.targetOffsets,
55
+ }),
56
+ ],
57
+ ],
58
+ })
59
+ }
@@ -0,0 +1,56 @@
1
+ import type {EditorSchema} from '@portabletext/editor'
2
+ import {raise} from '@portabletext/editor/behaviors'
3
+ import {getPreviousInlineObject} from '@portabletext/editor/selectors'
4
+ import {defineInputRule} from '@portabletext/plugin-input-rule'
5
+
6
+ export function createOrderedListRule(config: {
7
+ orderedList: (context: {schema: EditorSchema}) => string | undefined
8
+ }) {
9
+ return defineInputRule({
10
+ on: /^1\. /,
11
+ guard: ({snapshot, event}) => {
12
+ const orderedList = config.orderedList({
13
+ schema: snapshot.context.schema,
14
+ })
15
+
16
+ if (!orderedList) {
17
+ return false
18
+ }
19
+
20
+ const previousInlineObject = getPreviousInlineObject(snapshot)
21
+
22
+ if (previousInlineObject) {
23
+ return false
24
+ }
25
+
26
+ const match = event.matches.at(0)
27
+
28
+ if (!match) {
29
+ return false
30
+ }
31
+
32
+ return {match, orderedList}
33
+ },
34
+ actions: [
35
+ ({event}, {match, orderedList}) => [
36
+ raise({
37
+ type: 'block.unset',
38
+ props: ['style'],
39
+ at: event.focusTextBlock.path,
40
+ }),
41
+ raise({
42
+ type: 'block.set',
43
+ props: {
44
+ listItem: orderedList,
45
+ level: event.focusTextBlock.node.level ?? 1,
46
+ },
47
+ at: event.focusTextBlock.path,
48
+ }),
49
+ raise({
50
+ type: 'delete',
51
+ at: match.targetOffsets,
52
+ }),
53
+ ],
54
+ ],
55
+ })
56
+ }
@@ -0,0 +1,56 @@
1
+ import type {EditorSchema} from '@portabletext/editor'
2
+ import {raise} from '@portabletext/editor/behaviors'
3
+ import {getPreviousInlineObject} from '@portabletext/editor/selectors'
4
+ import {defineInputRule} from '@portabletext/plugin-input-rule'
5
+
6
+ export function createUnorderedListRule(config: {
7
+ unorderedList: (context: {schema: EditorSchema}) => string | undefined
8
+ }) {
9
+ return defineInputRule({
10
+ on: /^(-|\*) /,
11
+ guard: ({snapshot, event}) => {
12
+ const unorderedList = config.unorderedList({
13
+ schema: snapshot.context.schema,
14
+ })
15
+
16
+ if (!unorderedList) {
17
+ return false
18
+ }
19
+
20
+ const previousInlineObject = getPreviousInlineObject(snapshot)
21
+
22
+ if (previousInlineObject) {
23
+ return false
24
+ }
25
+
26
+ const match = event.matches.at(0)
27
+
28
+ if (!match) {
29
+ return false
30
+ }
31
+
32
+ return {match, unorderedList}
33
+ },
34
+ actions: [
35
+ ({event}, {match, unorderedList}) => [
36
+ raise({
37
+ type: 'block.unset',
38
+ props: ['style'],
39
+ at: event.focusTextBlock.path,
40
+ }),
41
+ raise({
42
+ type: 'block.set',
43
+ props: {
44
+ listItem: unorderedList,
45
+ level: event.focusTextBlock.node.level ?? 1,
46
+ },
47
+ at: event.focusTextBlock.path,
48
+ }),
49
+ raise({
50
+ type: 'delete',
51
+ at: match.targetOffsets,
52
+ }),
53
+ ],
54
+ ],
55
+ })
56
+ }