@portabletext/plugin-markdown-shortcuts 1.1.4 → 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,53 @@
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 createBlockquoteRule(config: {
7
+ blockquoteStyle: (context: {schema: EditorSchema}) => string | undefined
8
+ }) {
9
+ return defineInputRule({
10
+ on: /^> /,
11
+ guard: ({snapshot, event}) => {
12
+ const style = config.blockquoteStyle({
13
+ schema: snapshot.context.schema,
14
+ })
15
+
16
+ if (!style) {
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 {style, match}
33
+ },
34
+ actions: [
35
+ ({event}, {style, match}) => [
36
+ raise({
37
+ type: 'block.unset',
38
+ props: ['listItem', 'level'],
39
+ at: event.focusTextBlock.path,
40
+ }),
41
+ raise({
42
+ type: 'block.set',
43
+ props: {style},
44
+ at: event.focusTextBlock.path,
45
+ }),
46
+ raise({
47
+ type: 'delete',
48
+ at: match.targetOffsets,
49
+ }),
50
+ ],
51
+ ],
52
+ })
53
+ }
@@ -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,61 @@
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 createHorizontalRuleRule(config: {
7
+ horizontalRuleObject: (context: {
8
+ schema: EditorSchema
9
+ }) => {name: string; value?: {[prop: string]: unknown}} | undefined
10
+ }) {
11
+ return defineInputRule({
12
+ on: /^(---)|^(—-)|^(___)|^(\*\*\*)/,
13
+ guard: ({snapshot, event}) => {
14
+ const hrObject = config.horizontalRuleObject({
15
+ schema: snapshot.context.schema,
16
+ })
17
+
18
+ if (!hrObject) {
19
+ // If no horizontal rule object is provided, then we can safely skip
20
+ // this rule.
21
+ return false
22
+ }
23
+
24
+ const previousInlineObject = getPreviousInlineObject(snapshot)
25
+
26
+ if (previousInlineObject) {
27
+ // Input Rules only look at the plain text of the text block. This
28
+ // means that the RegExp is matched even if there is a leading inline
29
+ // object.
30
+ return false
31
+ }
32
+
33
+ // In theory, an Input Rule could return multiple matches. But in this
34
+ // case we only expect one match.
35
+ const match = event.matches.at(0)
36
+
37
+ if (!match) {
38
+ return false
39
+ }
40
+
41
+ return {hrObject, match}
42
+ },
43
+ actions: [
44
+ (_, {hrObject, match}) => [
45
+ raise({
46
+ type: 'insert.block',
47
+ block: {
48
+ _type: hrObject.name,
49
+ ...(hrObject.value ?? {}),
50
+ },
51
+ placement: 'before',
52
+ select: 'none',
53
+ }),
54
+ raise({
55
+ type: 'delete',
56
+ at: match.targetOffsets,
57
+ }),
58
+ ],
59
+ ],
60
+ })
61
+ }
@@ -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
+ }