@portabletext/plugin-one-line 1.0.19 → 1.1.1
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.cjs +44 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +44 -25
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var editor = require("@portabletext/editor"), behaviors = require("@portabletext/editor/behaviors"), selectors = require("@portabletext/editor/selectors"), utils = require("@portabletext/editor/utils"), react = require("react");
|
|
3
|
+
var compilerRuntime = require("react/compiler-runtime"), editor = require("@portabletext/editor"), behaviors = require("@portabletext/editor/behaviors"), selectors = require("@portabletext/editor/selectors"), utils = require("@portabletext/editor/utils"), react = require("react");
|
|
4
4
|
function _interopNamespaceCompat(e) {
|
|
5
5
|
if (e && typeof e == "object" && "default" in e) return e;
|
|
6
6
|
var n = /* @__PURE__ */ Object.create(null);
|
|
@@ -24,8 +24,17 @@ const oneLineBehaviors = [
|
|
|
24
24
|
*/
|
|
25
25
|
behaviors.defineBehavior({
|
|
26
26
|
on: "insert.break",
|
|
27
|
-
guard: ({
|
|
28
|
-
|
|
27
|
+
guard: ({
|
|
28
|
+
snapshot
|
|
29
|
+
}) => snapshot.context.selection && selectors__namespace.isSelectionExpanded(snapshot) ? {
|
|
30
|
+
selection: snapshot.context.selection
|
|
31
|
+
} : !1,
|
|
32
|
+
actions: [(_, {
|
|
33
|
+
selection
|
|
34
|
+
}) => [behaviors.execute({
|
|
35
|
+
type: "delete",
|
|
36
|
+
at: selection
|
|
37
|
+
})]]
|
|
29
38
|
}),
|
|
30
39
|
/**
|
|
31
40
|
* All other cases of `insert.break` should be aborted.
|
|
@@ -39,7 +48,9 @@ const oneLineBehaviors = [
|
|
|
39
48
|
*/
|
|
40
49
|
behaviors.defineBehavior({
|
|
41
50
|
on: "insert.block",
|
|
42
|
-
guard: ({
|
|
51
|
+
guard: ({
|
|
52
|
+
event
|
|
53
|
+
}) => event.placement === "before" || event.placement === "after",
|
|
43
54
|
actions: []
|
|
44
55
|
}),
|
|
45
56
|
/**
|
|
@@ -48,17 +59,18 @@ const oneLineBehaviors = [
|
|
|
48
59
|
*/
|
|
49
60
|
behaviors.defineBehavior({
|
|
50
61
|
on: "insert.block",
|
|
51
|
-
guard: ({
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
+
guard: ({
|
|
63
|
+
snapshot,
|
|
64
|
+
event
|
|
65
|
+
}) => !(!selectors__namespace.getFocusTextBlock(snapshot) || !utils__namespace.isTextBlock(snapshot.context, event.block)),
|
|
66
|
+
actions: [({
|
|
67
|
+
event
|
|
68
|
+
}) => [behaviors.execute({
|
|
69
|
+
type: "insert.block",
|
|
70
|
+
block: event.block,
|
|
71
|
+
placement: "auto",
|
|
72
|
+
select: "end"
|
|
73
|
+
})]]
|
|
62
74
|
}),
|
|
63
75
|
/**
|
|
64
76
|
* Fallback Behavior to avoid `insert.block` in case the Behaviors above all
|
|
@@ -74,10 +86,11 @@ const oneLineBehaviors = [
|
|
|
74
86
|
*/
|
|
75
87
|
behaviors.defineBehavior({
|
|
76
88
|
on: "insert.blocks",
|
|
77
|
-
guard: ({
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
89
|
+
guard: ({
|
|
90
|
+
snapshot,
|
|
91
|
+
event
|
|
92
|
+
}) => {
|
|
93
|
+
const textBlocks = event.blocks.filter((block) => utils__namespace.isTextBlock(snapshot.context, block));
|
|
81
94
|
return textBlocks.length === 0 ? !1 : textBlocks.reduce((targetBlock, incomingBlock) => utils__namespace.mergeTextBlocks({
|
|
82
95
|
context: snapshot.context,
|
|
83
96
|
targetBlock,
|
|
@@ -87,7 +100,11 @@ const oneLineBehaviors = [
|
|
|
87
100
|
actions: [
|
|
88
101
|
// `insert.block` is raised so the Behavior above can handle the
|
|
89
102
|
// insertion
|
|
90
|
-
(_, block) => [behaviors.raise({
|
|
103
|
+
(_, block) => [behaviors.raise({
|
|
104
|
+
type: "insert.block",
|
|
105
|
+
block,
|
|
106
|
+
placement: "auto"
|
|
107
|
+
})]
|
|
91
108
|
]
|
|
92
109
|
}),
|
|
93
110
|
/**
|
|
@@ -100,16 +117,17 @@ const oneLineBehaviors = [
|
|
|
100
117
|
})
|
|
101
118
|
];
|
|
102
119
|
function OneLinePlugin() {
|
|
103
|
-
const editor$1 = editor.useEditor();
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
120
|
+
const $ = compilerRuntime.c(3), editor$1 = editor.useEditor();
|
|
121
|
+
let t0, t1;
|
|
122
|
+
return $[0] !== editor$1 ? (t0 = () => {
|
|
123
|
+
const unregisterBehaviors = oneLineBehaviors.map((behavior) => editor$1.registerBehavior({
|
|
124
|
+
behavior
|
|
125
|
+
}));
|
|
108
126
|
return () => {
|
|
109
127
|
for (const unregisterBehavior of unregisterBehaviors)
|
|
110
128
|
unregisterBehavior();
|
|
111
129
|
};
|
|
112
|
-
}, [editor$1]), null;
|
|
130
|
+
}, t1 = [editor$1], $[0] = editor$1, $[1] = t0, $[2] = t1) : (t0 = $[1], t1 = $[2]), react.useEffect(t0, t1), null;
|
|
113
131
|
}
|
|
114
132
|
exports.OneLinePlugin = OneLinePlugin;
|
|
115
133
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/plugin.one-line.tsx"],"sourcesContent":["import {useEditor} from '@portabletext/editor'\nimport {defineBehavior, execute, 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}) => [execute({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 true\n },\n actions: [\n ({event}) => [\n execute({\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 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 * @beta\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":["defineBehavior","selectors","execute","utils","raise","editor","useEditor","useEffect"],"mappings":";;;;;;;;;;;;;;;;;;;AAMA,
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/plugin.one-line.tsx"],"sourcesContent":["import {useEditor} from '@portabletext/editor'\nimport {defineBehavior, execute, 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}) => [execute({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 true\n },\n actions: [\n ({event}) => [\n execute({\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 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 * @beta\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","_","execute","type","at","event","placement","getFocusTextBlock","utils","isTextBlock","block","select","textBlocks","blocks","filter","length","reduce","targetBlock","incomingBlock","mergeTextBlocks","raise","OneLinePlugin","$","_c","editor","useEditor","t0","t1","unregisterBehaviors","map","behavior","registerBehavior","unregisterBehavior","useEffect"],"mappings":";;;;;;;;;;;;;;;;;;;AAMA,MAAMA,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKvBC,yBAAe;AAAA,IACbC,IAAI;AAAA,IACJC,OAAOA,CAAC;AAAA,MAACC;AAAAA,IAAAA,MACPA,SAASC,QAAQC,aAAaC,qBAAUC,oBAAoBJ,QAAQ,IAChE;AAAA,MAACE,WAAWF,SAASC,QAAQC;AAAAA,IAAAA,IAC7B;AAAA,IACNG,SAAS,CAAC,CAACC,GAAG;AAAA,MAACJ;AAAAA,IAAAA,MAAe,CAACK,UAAAA,QAAQ;AAAA,MAACC,MAAM;AAAA,MAAUC,IAAIP;AAAAA,IAAAA,CAAU,CAAC,CAAC;AAAA,EAAA,CACzE;AAAA;AAAA;AAAA;AAAA,EAIDL,yBAAe;AAAA,IACbC,IAAI;AAAA,IACJO,SAAS,CAAA;AAAA,EAAA,CACV;AAAA;AAAA;AAAA;AAAA,EAIDR,yBAAe;AAAA,IACbC,IAAI;AAAA,IACJC,OAAOA,CAAC;AAAA,MAACW;AAAAA,IAAAA,MACPA,MAAMC,cAAc,YAAYD,MAAMC,cAAc;AAAA,IACtDN,SAAS,CAAA;AAAA,EAAA,CACV;AAAA;AAAA;AAAA;AAAA;AAAA,EAKDR,yBAAe;AAAA,IACbC,IAAI;AAAA,IACJC,OAAOA,CAAC;AAAA,MAACC;AAAAA,MAAUU;AAAAA,IAAAA,MAIf,EAAA,CAHqBP,qBAAUS,kBAAkBZ,QAAQ,KAIzD,CAACa,iBAAMC,YAAYd,SAASC,SAASS,MAAMK,KAAK;AAAA,IAOpDV,SAAS,CACP,CAAC;AAAA,MAACK;AAAAA,IAAAA,MAAW,CACXH,UAAAA,QAAQ;AAAA,MACNC,MAAM;AAAA,MACNO,OAAOL,MAAMK;AAAAA,MACbJ,W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A,EAAA,GAGvBL,MAACH,QAAM,GAACF,OAAAE,UAAAF,OAAAI,IAAAJ,OAAAK,OAAAD,KAAAJ,EAAA,CAAA,GAAAK,KAAAL,EAAA,CAAA,IAVXW,MAAAA,UAAUP,IAUPC,EAAQ,GAAC;AAAA;;"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { c } from "react/compiler-runtime";
|
|
1
2
|
import { useEditor } from "@portabletext/editor";
|
|
2
3
|
import { defineBehavior, execute, raise } from "@portabletext/editor/behaviors";
|
|
3
4
|
import * as selectors from "@portabletext/editor/selectors";
|
|
@@ -10,8 +11,17 @@ const oneLineBehaviors = [
|
|
|
10
11
|
*/
|
|
11
12
|
defineBehavior({
|
|
12
13
|
on: "insert.break",
|
|
13
|
-
guard: ({
|
|
14
|
-
|
|
14
|
+
guard: ({
|
|
15
|
+
snapshot
|
|
16
|
+
}) => snapshot.context.selection && selectors.isSelectionExpanded(snapshot) ? {
|
|
17
|
+
selection: snapshot.context.selection
|
|
18
|
+
} : !1,
|
|
19
|
+
actions: [(_, {
|
|
20
|
+
selection
|
|
21
|
+
}) => [execute({
|
|
22
|
+
type: "delete",
|
|
23
|
+
at: selection
|
|
24
|
+
})]]
|
|
15
25
|
}),
|
|
16
26
|
/**
|
|
17
27
|
* All other cases of `insert.break` should be aborted.
|
|
@@ -25,7 +35,9 @@ const oneLineBehaviors = [
|
|
|
25
35
|
*/
|
|
26
36
|
defineBehavior({
|
|
27
37
|
on: "insert.block",
|
|
28
|
-
guard: ({
|
|
38
|
+
guard: ({
|
|
39
|
+
event
|
|
40
|
+
}) => event.placement === "before" || event.placement === "after",
|
|
29
41
|
actions: []
|
|
30
42
|
}),
|
|
31
43
|
/**
|
|
@@ -34,17 +46,18 @@ const oneLineBehaviors = [
|
|
|
34
46
|
*/
|
|
35
47
|
defineBehavior({
|
|
36
48
|
on: "insert.block",
|
|
37
|
-
guard: ({
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
guard: ({
|
|
50
|
+
snapshot,
|
|
51
|
+
event
|
|
52
|
+
}) => !(!selectors.getFocusTextBlock(snapshot) || !utils.isTextBlock(snapshot.context, event.block)),
|
|
53
|
+
actions: [({
|
|
54
|
+
event
|
|
55
|
+
}) => [execute({
|
|
56
|
+
type: "insert.block",
|
|
57
|
+
block: event.block,
|
|
58
|
+
placement: "auto",
|
|
59
|
+
select: "end"
|
|
60
|
+
})]]
|
|
48
61
|
}),
|
|
49
62
|
/**
|
|
50
63
|
* Fallback Behavior to avoid `insert.block` in case the Behaviors above all
|
|
@@ -60,10 +73,11 @@ const oneLineBehaviors = [
|
|
|
60
73
|
*/
|
|
61
74
|
defineBehavior({
|
|
62
75
|
on: "insert.blocks",
|
|
63
|
-
guard: ({
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
76
|
+
guard: ({
|
|
77
|
+
snapshot,
|
|
78
|
+
event
|
|
79
|
+
}) => {
|
|
80
|
+
const textBlocks = event.blocks.filter((block) => utils.isTextBlock(snapshot.context, block));
|
|
67
81
|
return textBlocks.length === 0 ? !1 : textBlocks.reduce((targetBlock, incomingBlock) => utils.mergeTextBlocks({
|
|
68
82
|
context: snapshot.context,
|
|
69
83
|
targetBlock,
|
|
@@ -73,7 +87,11 @@ const oneLineBehaviors = [
|
|
|
73
87
|
actions: [
|
|
74
88
|
// `insert.block` is raised so the Behavior above can handle the
|
|
75
89
|
// insertion
|
|
76
|
-
(_, block) => [raise({
|
|
90
|
+
(_, block) => [raise({
|
|
91
|
+
type: "insert.block",
|
|
92
|
+
block,
|
|
93
|
+
placement: "auto"
|
|
94
|
+
})]
|
|
77
95
|
]
|
|
78
96
|
}),
|
|
79
97
|
/**
|
|
@@ -86,16 +104,17 @@ const oneLineBehaviors = [
|
|
|
86
104
|
})
|
|
87
105
|
];
|
|
88
106
|
function OneLinePlugin() {
|
|
89
|
-
const editor = useEditor();
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
107
|
+
const $ = c(3), editor = useEditor();
|
|
108
|
+
let t0, t1;
|
|
109
|
+
return $[0] !== editor ? (t0 = () => {
|
|
110
|
+
const unregisterBehaviors = oneLineBehaviors.map((behavior) => editor.registerBehavior({
|
|
111
|
+
behavior
|
|
112
|
+
}));
|
|
94
113
|
return () => {
|
|
95
114
|
for (const unregisterBehavior of unregisterBehaviors)
|
|
96
115
|
unregisterBehavior();
|
|
97
116
|
};
|
|
98
|
-
}, [editor]), null;
|
|
117
|
+
}, t1 = [editor], $[0] = editor, $[1] = t0, $[2] = t1) : (t0 = $[1], t1 = $[2]), useEffect(t0, t1), null;
|
|
99
118
|
}
|
|
100
119
|
export {
|
|
101
120
|
OneLinePlugin
|
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, execute, 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}) => [execute({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 true\n },\n actions: [\n ({event}) => [\n execute({\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 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 * @beta\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":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/plugin.one-line.tsx"],"sourcesContent":["import {useEditor} from '@portabletext/editor'\nimport {defineBehavior, execute, 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}) => [execute({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 true\n },\n actions: [\n ({event}) => [\n execute({\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 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 * @beta\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","_","execute","type","at","event","placement","getFocusTextBlock","utils","isTextBlock","block","select","textBlocks","blocks","filter","length","reduce","targetBlock","incomingBlock","mergeTextBlocks","raise","OneLinePlugin","$","_c","editor","useEditor","t0","t1","unregisterBehaviors","map","behavior","registerBehavior","unregisterBehavior","useEffect"],"mappings":";;;;;;AAMA,MAAMA,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKvBC,eAAe;AAAA,IACbC,IAAI;AAAA,IACJC,OAAOA,CAAC;AAAA,MAACC;AAAAA,IAAAA,MACPA,SAASC,QAAQC,aAAaC,UAAUC,oBAAoBJ,QAAQ,IAChE;AAAA,MAACE,WAAWF,SAASC,QAAQC;AAAAA,IAAAA,IAC7B;AAAA,IACNG,SAAS,CAAC,CAACC,GAAG;AAAA,MAACJ;AAAAA,IAAAA,MAAe,CAACK,QAAQ;AAAA,MAACC,MAAM;AAAA,MAAUC,IAAIP;AAAAA,IAAAA,CAAU,CAAC,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AA,MAAAA,CACD,CACF;AAAA,IACH;AAAA,IACAlB,SAAS;AAAA;AAAA;AAAA,MAGP,CAACC,GAAGS,UAAU,CAACU,MAAM;AAAA,QAACjB,MAAM;AAAA,QAAgBO;AAAAA,QAAOJ,WAAW;AAAA,MAAA,CAAO,CAAC;AAAA,IAAA;AAAA,EAAC,CAE1E;AAAA;AAAA;AAAA;AAAA;AAAA,EAKDd,eAAe;AAAA,IACbC,IAAI;AAAA,IACJO,SAAS,CAAA;AAAA,EAAA,CACV;AAAC;AAWG,SAAAqB,gBAAA;AAAA,QAAAC,IAAAC,EAAA,CAAA,GACLC,SAAeC,UAAAA;AAAW,MAAAC,IAAAC;AAAA,SAAAL,SAAAE,UAEhBE,KAAAA,MAAA;AACR,UAAAE,sBAA4BrC,iBAAAsC,IAAAC,CAAAA,aAC1BN,OAAMO,iBAAA;AAAA,MAAAD;AAAAA,IAAAA,CAA4B,CACpC;AAAC,WAAA,MAAA;AAAA,iBAGME,sBAA4BJ;AAC/BI,2BAAAA;AAAAA,IAAoB;AAAA,EAAA,GAGvBL,MAACH,MAAM,GAACF,OAAAE,QAAAF,OAAAI,IAAAJ,OAAAK,OAAAD,KAAAJ,EAAA,CAAA,GAAAK,KAAAL,EAAA,CAAA,IAVXW,UAAUP,IAUPC,EAAQ,GAAC;AAAA;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portabletext/plugin-one-line",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "🤏 Restricts the Portable Text Editor to a single line",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"portabletext",
|
|
@@ -38,18 +38,19 @@
|
|
|
38
38
|
"dist"
|
|
39
39
|
],
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@portabletext/editor": "^2.
|
|
41
|
+
"@portabletext/editor": "^2.6.9",
|
|
42
42
|
"@types/react": "^19.1.2",
|
|
43
|
+
"babel-plugin-react-compiler": "19.1.0-rc.3",
|
|
43
44
|
"react": "^19.1.0"
|
|
44
45
|
},
|
|
45
46
|
"peerDependencies": {
|
|
46
|
-
"@portabletext/editor": "^2.
|
|
47
|
-
"react": "^19
|
|
47
|
+
"@portabletext/editor": "^2.6.9",
|
|
48
|
+
"react": "^19"
|
|
48
49
|
},
|
|
49
50
|
"scripts": {
|
|
50
51
|
"build": "pkg-utils build --strict --check --clean",
|
|
51
52
|
"check:lint": "biome lint .",
|
|
52
|
-
"check:react-compiler": "eslint
|
|
53
|
+
"check:react-compiler": "eslint .",
|
|
53
54
|
"check:types": "tsc",
|
|
54
55
|
"check:types:watch": "tsc --watch",
|
|
55
56
|
"clean": "del .turbo && del dist && del node_modules",
|