@portabletext/plugin-one-line 2.0.1 → 2.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/dist/index.d.ts +1 -1
- package/dist/index.js +8 -4
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/plugin.one-line.tsx +14 -5
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { c } from "react/compiler-runtime";
|
|
2
2
|
import { useEditor } from "@portabletext/editor";
|
|
3
|
-
import { defineBehavior,
|
|
3
|
+
import { defineBehavior, raise } from "@portabletext/editor/behaviors";
|
|
4
4
|
import * as selectors from "@portabletext/editor/selectors";
|
|
5
5
|
import * as utils from "@portabletext/editor/utils";
|
|
6
6
|
import { useEffect } from "react";
|
|
@@ -18,7 +18,7 @@ const oneLineBehaviors = [
|
|
|
18
18
|
} : !1,
|
|
19
19
|
actions: [(_, {
|
|
20
20
|
selection
|
|
21
|
-
}) => [
|
|
21
|
+
}) => [raise({
|
|
22
22
|
type: "delete",
|
|
23
23
|
at: selection
|
|
24
24
|
})]]
|
|
@@ -49,10 +49,10 @@ const oneLineBehaviors = [
|
|
|
49
49
|
guard: ({
|
|
50
50
|
snapshot,
|
|
51
51
|
event
|
|
52
|
-
}) => !
|
|
52
|
+
}) => !selectors.getFocusTextBlock(snapshot) || !utils.isTextBlock(snapshot.context, event.block) ? !1 : event.placement !== "auto" || event.select !== "end",
|
|
53
53
|
actions: [({
|
|
54
54
|
event
|
|
55
|
-
}) => [
|
|
55
|
+
}) => [raise({
|
|
56
56
|
type: "insert.block",
|
|
57
57
|
block: event.block,
|
|
58
58
|
placement: "auto",
|
|
@@ -65,6 +65,10 @@ const oneLineBehaviors = [
|
|
|
65
65
|
*/
|
|
66
66
|
defineBehavior({
|
|
67
67
|
on: "insert.block",
|
|
68
|
+
guard: ({
|
|
69
|
+
snapshot,
|
|
70
|
+
event
|
|
71
|
+
}) => selectors.getFocusTextBlock(snapshot) ? !utils.isTextBlock(snapshot.context, event.block) : !0,
|
|
68
72
|
actions: []
|
|
69
73
|
}),
|
|
70
74
|
/**
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/plugin.one-line.tsx"],"sourcesContent":["import {useEditor} from '@portabletext/editor'\nimport {defineBehavior,
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/plugin.one-line.tsx"],"sourcesContent":["import {useEditor} from '@portabletext/editor'\nimport {defineBehavior, raise} from '@portabletext/editor/behaviors'\nimport * as selectors from '@portabletext/editor/selectors'\nimport * as utils from '@portabletext/editor/utils'\nimport {useEffect} from 'react'\n\nconst oneLineBehaviors = [\n /**\n * Hitting Enter on an expanded selection should just delete that selection\n * without causing a line break.\n */\n defineBehavior({\n on: 'insert.break',\n guard: ({snapshot}) =>\n snapshot.context.selection && selectors.isSelectionExpanded(snapshot)\n ? {selection: snapshot.context.selection}\n : false,\n actions: [(_, {selection}) => [raise({type: 'delete', at: selection})]],\n }),\n /**\n * All other cases of `insert.break` should be aborted.\n */\n defineBehavior({\n on: 'insert.break',\n actions: [],\n }),\n /**\n * `insert.block` `before` or `after` is not allowed in a one-line editor.\n */\n defineBehavior({\n on: 'insert.block',\n guard: ({event}) =>\n event.placement === 'before' || event.placement === 'after',\n actions: [],\n }),\n /**\n * An ordinary `insert.block` is acceptable if it's a text block. In that\n * case it will get merged into the existing text block.\n */\n defineBehavior({\n on: 'insert.block',\n guard: ({snapshot, event}) => {\n const focusTextBlock = selectors.getFocusTextBlock(snapshot)\n\n if (\n !focusTextBlock ||\n !utils.isTextBlock(snapshot.context, event.block)\n ) {\n return false\n }\n\n return event.placement !== 'auto' || event.select !== 'end'\n },\n actions: [\n ({event}) => [\n raise({\n type: 'insert.block',\n block: event.block,\n placement: 'auto',\n select: 'end',\n }),\n ],\n ],\n }),\n /**\n * Fallback Behavior to avoid `insert.block` in case the Behaviors above all\n * end up with a falsy guard.\n */\n defineBehavior({\n on: 'insert.block',\n guard: ({snapshot, event}) => {\n const focusTextBlock = selectors.getFocusTextBlock(snapshot)\n\n if (!focusTextBlock) {\n return true\n }\n\n return !utils.isTextBlock(snapshot.context, event.block)\n },\n actions: [],\n }),\n /**\n * If multiple blocks are inserted, then the non-text blocks are filtered out\n * and the text blocks are merged into one block\n */\n defineBehavior({\n on: 'insert.blocks',\n guard: ({snapshot, event}) => {\n const textBlocks = event.blocks.filter((block) =>\n utils.isTextBlock(snapshot.context, block),\n )\n\n if (textBlocks.length === 0) {\n return false\n }\n\n return textBlocks.reduce((targetBlock, incomingBlock) => {\n return utils.mergeTextBlocks({\n context: snapshot.context,\n targetBlock,\n incomingBlock,\n })\n })\n },\n actions: [\n // `insert.block` is raised so the Behavior above can handle the\n // insertion\n (_, block) => [raise({type: 'insert.block', block, placement: 'auto'})],\n ],\n }),\n /**\n * Fallback Behavior to avoid `insert.blocks` in case the Behavior above\n * ends up with a falsy guard.\n */\n defineBehavior({\n on: 'insert.blocks',\n actions: [],\n }),\n]\n\n/**\n * @public\n * Restrict the editor to one line. The plugin takes care of blocking\n * `insert.break` events and smart handling of other `insert.*` events.\n *\n * Place it with as high priority as possible to make sure other plugins don't\n * overwrite `insert.*` events before this plugin gets a chance to do so.\n */\nexport function OneLinePlugin() {\n const editor = useEditor()\n\n useEffect(() => {\n const unregisterBehaviors = oneLineBehaviors.map((behavior) =>\n editor.registerBehavior({behavior}),\n )\n\n return () => {\n for (const unregisterBehavior of unregisterBehaviors) {\n unregisterBehavior()\n }\n }\n }, [editor])\n\n return null\n}\n"],"names":["oneLineBehaviors","defineBehavior","on","guard","snapshot","context","selection","selectors","isSelectionExpanded","actions","_","raise","type","at","event","placement","getFocusTextBlock","utils","isTextBlock","block","select","textBlocks","blocks","filter","length","reduce","targetBlock","incomingBlock","mergeTextBlocks","OneLinePlugin","$","_c","editor","useEditor","t0","t1","unregisterBehaviors","map","behavior","registerBehavior","unregisterB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;AAC/BI,2BAAAA;AAAAA,IACD;AAAA,EACF,GACAL,KAAA,CAACH,MAAM,GAACF,OAAAE,QAAAF,OAAAI,IAAAJ,OAAAK,OAAAD,KAAAJ,EAAA,CAAA,GAAAK,KAAAL,EAAA,CAAA,IAVXW,UAAUP,IAUPC,EAAQ,GAEJ;AAAI;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portabletext/plugin-one-line",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "🤏 Restricts the Portable Text Editor to a single line",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"portabletext",
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
"react": "^19.2.0",
|
|
43
43
|
"typescript": "5.9.3",
|
|
44
44
|
"typescript-eslint": "^8.46.1",
|
|
45
|
-
"@portabletext/editor": "^2.
|
|
45
|
+
"@portabletext/editor": "^2.19.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"react": "^19",
|
|
49
|
-
"@portabletext/editor": "^2.
|
|
49
|
+
"@portabletext/editor": "^2.19.0"
|
|
50
50
|
},
|
|
51
51
|
"engines": {
|
|
52
52
|
"node": ">=20.19 <22 || >=22.12"
|
package/src/plugin.one-line.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {useEditor} from '@portabletext/editor'
|
|
2
|
-
import {defineBehavior,
|
|
2
|
+
import {defineBehavior, raise} from '@portabletext/editor/behaviors'
|
|
3
3
|
import * as selectors from '@portabletext/editor/selectors'
|
|
4
4
|
import * as utils from '@portabletext/editor/utils'
|
|
5
5
|
import {useEffect} from 'react'
|
|
@@ -15,7 +15,7 @@ const oneLineBehaviors = [
|
|
|
15
15
|
snapshot.context.selection && selectors.isSelectionExpanded(snapshot)
|
|
16
16
|
? {selection: snapshot.context.selection}
|
|
17
17
|
: false,
|
|
18
|
-
actions: [(_, {selection}) => [
|
|
18
|
+
actions: [(_, {selection}) => [raise({type: 'delete', at: selection})]],
|
|
19
19
|
}),
|
|
20
20
|
/**
|
|
21
21
|
* All other cases of `insert.break` should be aborted.
|
|
@@ -49,11 +49,11 @@ const oneLineBehaviors = [
|
|
|
49
49
|
return false
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
return
|
|
52
|
+
return event.placement !== 'auto' || event.select !== 'end'
|
|
53
53
|
},
|
|
54
54
|
actions: [
|
|
55
55
|
({event}) => [
|
|
56
|
-
|
|
56
|
+
raise({
|
|
57
57
|
type: 'insert.block',
|
|
58
58
|
block: event.block,
|
|
59
59
|
placement: 'auto',
|
|
@@ -68,6 +68,15 @@ const oneLineBehaviors = [
|
|
|
68
68
|
*/
|
|
69
69
|
defineBehavior({
|
|
70
70
|
on: 'insert.block',
|
|
71
|
+
guard: ({snapshot, event}) => {
|
|
72
|
+
const focusTextBlock = selectors.getFocusTextBlock(snapshot)
|
|
73
|
+
|
|
74
|
+
if (!focusTextBlock) {
|
|
75
|
+
return true
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return !utils.isTextBlock(snapshot.context, event.block)
|
|
79
|
+
},
|
|
71
80
|
actions: [],
|
|
72
81
|
}),
|
|
73
82
|
/**
|
|
@@ -110,7 +119,7 @@ const oneLineBehaviors = [
|
|
|
110
119
|
]
|
|
111
120
|
|
|
112
121
|
/**
|
|
113
|
-
* @
|
|
122
|
+
* @public
|
|
114
123
|
* Restrict the editor to one line. The plugin takes care of blocking
|
|
115
124
|
* `insert.break` events and smart handling of other `insert.*` events.
|
|
116
125
|
*
|