@portabletext/plugin-one-line 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 - 2025 Sanity.io
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # One-Line Plugin
2
+
3
+ > 🤏 Restricts the Portable Text Editor to a single line
package/dist/index.cjs ADDED
@@ -0,0 +1,115 @@
1
+ "use strict";
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");
4
+ function _interopNamespaceCompat(e) {
5
+ if (e && typeof e == "object" && "default" in e) return e;
6
+ var n = /* @__PURE__ */ Object.create(null);
7
+ return e && Object.keys(e).forEach(function(k) {
8
+ if (k !== "default") {
9
+ var d = Object.getOwnPropertyDescriptor(e, k);
10
+ Object.defineProperty(n, k, d.get ? d : {
11
+ enumerable: !0,
12
+ get: function() {
13
+ return e[k];
14
+ }
15
+ });
16
+ }
17
+ }), n.default = e, Object.freeze(n);
18
+ }
19
+ var selectors__namespace = /* @__PURE__ */ _interopNamespaceCompat(selectors), utils__namespace = /* @__PURE__ */ _interopNamespaceCompat(utils);
20
+ const oneLineBehaviors = [
21
+ /**
22
+ * Hitting Enter on an expanded selection should just delete that selection
23
+ * without causing a line break.
24
+ */
25
+ behaviors.defineBehavior({
26
+ on: "insert.break",
27
+ guard: ({ snapshot }) => snapshot.context.selection && selectors__namespace.isSelectionExpanded(snapshot) ? { selection: snapshot.context.selection } : !1,
28
+ actions: [(_, { selection }) => [behaviors.execute({ type: "delete", at: selection })]]
29
+ }),
30
+ /**
31
+ * All other cases of `insert.break` should be aborted.
32
+ */
33
+ behaviors.defineBehavior({
34
+ on: "insert.break",
35
+ actions: []
36
+ }),
37
+ /**
38
+ * `insert.block` `before` or `after` is not allowed in a one-line editor.
39
+ */
40
+ behaviors.defineBehavior({
41
+ on: "insert.block",
42
+ guard: ({ event }) => event.placement === "before" || event.placement === "after",
43
+ actions: []
44
+ }),
45
+ /**
46
+ * An ordinary `insert.block` is acceptable if it's a text block. In that
47
+ * case it will get merged into the existing text block.
48
+ */
49
+ behaviors.defineBehavior({
50
+ on: "insert.block",
51
+ guard: ({ snapshot, event }) => !(!selectors__namespace.getFocusTextBlock(snapshot) || !utils__namespace.isTextBlock(snapshot.context, event.block)),
52
+ actions: [
53
+ ({ event }) => [
54
+ behaviors.execute({
55
+ type: "insert.block",
56
+ block: event.block,
57
+ placement: "auto",
58
+ select: "end"
59
+ })
60
+ ]
61
+ ]
62
+ }),
63
+ /**
64
+ * Fallback Behavior to avoid `insert.block` in case the Behaviors above all
65
+ * end up with a falsy guard.
66
+ */
67
+ behaviors.defineBehavior({
68
+ on: "insert.block",
69
+ actions: []
70
+ }),
71
+ /**
72
+ * If multiple blocks are inserted, then the non-text blocks are filtered out
73
+ * and the text blocks are merged into one block
74
+ */
75
+ behaviors.defineBehavior({
76
+ on: "insert.blocks",
77
+ guard: ({ snapshot, event }) => {
78
+ const textBlocks = event.blocks.filter(
79
+ (block) => utils__namespace.isTextBlock(snapshot.context, block)
80
+ );
81
+ return textBlocks.length === 0 ? !1 : textBlocks.reduce((targetBlock, incomingBlock) => utils__namespace.mergeTextBlocks({
82
+ context: snapshot.context,
83
+ targetBlock,
84
+ incomingBlock
85
+ }));
86
+ },
87
+ actions: [
88
+ // `insert.block` is raised so the Behavior above can handle the
89
+ // insertion
90
+ (_, block) => [behaviors.raise({ type: "insert.block", block, placement: "auto" })]
91
+ ]
92
+ }),
93
+ /**
94
+ * Fallback Behavior to avoid `insert.blocks` in case the Behavior above
95
+ * ends up with a falsy guard.
96
+ */
97
+ behaviors.defineBehavior({
98
+ on: "insert.blocks",
99
+ actions: []
100
+ })
101
+ ];
102
+ function OneLinePlugin() {
103
+ const editor$1 = editor.useEditor();
104
+ return react.useEffect(() => {
105
+ const unregisterBehaviors = oneLineBehaviors.map(
106
+ (behavior) => editor$1.registerBehavior({ behavior })
107
+ );
108
+ return () => {
109
+ for (const unregisterBehavior of unregisterBehaviors)
110
+ unregisterBehavior();
111
+ };
112
+ }, [editor$1]), null;
113
+ }
114
+ exports.OneLinePlugin = OneLinePlugin;
115
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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,MAAM,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKvBA,yBAAe;AAAA,IACb,IAAI;AAAA,IACJ,OAAO,CAAC,EAAC,SAAQ,MACf,SAAS,QAAQ,aAAaC,qBAAU,oBAAoB,QAAQ,IAChE,EAAC,WAAW,SAAS,QAAQ,cAC7B;AAAA,IACN,SAAS,CAAC,CAAC,GAAG,EAAC,gBAAe,CAACC,kBAAQ,EAAC,MAAM,UAAU,IAAI,UAAU,CAAA,CAAC,CAAC;AAAA,EAAA,CACzE;AAAA;AAAA;AAAA;AAAA,EAIDF,yBAAe;AAAA,IACb,IAAI;AAAA,IACJ,SAAS,CAAA;AAAA,EAAC,CACX;AAAA;AAAA;AAAA;AAAA,EAIDA,yBAAe;AAAA,IACb,IAAI;AAAA,IACJ,OAAO,CAAC,EAAC,YACP,MAAM,cAAc,YAAY,MAAM,cAAc;AAAA,IACtD,SAAS,CAAA;AAAA,EAAC,CACX;AAAA;AAAA;AAAA;AAAA;AAAA,EAKDA,yBAAe;AAAA,IACb,IAAI;AAAA,IACJ,OAAO,CAAC,EAAC,UAAU,MAAA,MAIf,EAHqB,CAAAC,qBAAU,kBAAkB,QAAQ,KAIzD,CAACE,iBAAM,YAAY,SAAS,SAAS,MAAM,KAAK;AAAA,IAOpD,SAAS;AAAA,MACP,CAAC,EAAC,MAAA,MAAW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one line. The plugin takes care of blocking
4
+ * `insert.break` events and smart handling of other `insert.*` events.
5
+ *
6
+ * Place it with as high priority as possible to make sure other plugins don't
7
+ * overwrite `insert.*` events before this plugin gets a chance to do so.
8
+ */
9
+ export declare function OneLinePlugin(): null
10
+
11
+ export {}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @beta
3
+ * Restrict the editor to one line. The plugin takes care of blocking
4
+ * `insert.break` events and smart handling of other `insert.*` events.
5
+ *
6
+ * Place it with as high priority as possible to make sure other plugins don't
7
+ * overwrite `insert.*` events before this plugin gets a chance to do so.
8
+ */
9
+ export declare function OneLinePlugin(): null
10
+
11
+ export {}
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ import { useEditor } from "@portabletext/editor";
2
+ import { defineBehavior, execute, raise } from "@portabletext/editor/behaviors";
3
+ import * as selectors from "@portabletext/editor/selectors";
4
+ import * as utils from "@portabletext/editor/utils";
5
+ import { useEffect } from "react";
6
+ const oneLineBehaviors = [
7
+ /**
8
+ * Hitting Enter on an expanded selection should just delete that selection
9
+ * without causing a line break.
10
+ */
11
+ defineBehavior({
12
+ on: "insert.break",
13
+ guard: ({ snapshot }) => snapshot.context.selection && selectors.isSelectionExpanded(snapshot) ? { selection: snapshot.context.selection } : !1,
14
+ actions: [(_, { selection }) => [execute({ type: "delete", at: selection })]]
15
+ }),
16
+ /**
17
+ * All other cases of `insert.break` should be aborted.
18
+ */
19
+ defineBehavior({
20
+ on: "insert.break",
21
+ actions: []
22
+ }),
23
+ /**
24
+ * `insert.block` `before` or `after` is not allowed in a one-line editor.
25
+ */
26
+ defineBehavior({
27
+ on: "insert.block",
28
+ guard: ({ event }) => event.placement === "before" || event.placement === "after",
29
+ actions: []
30
+ }),
31
+ /**
32
+ * An ordinary `insert.block` is acceptable if it's a text block. In that
33
+ * case it will get merged into the existing text block.
34
+ */
35
+ defineBehavior({
36
+ on: "insert.block",
37
+ guard: ({ snapshot, event }) => !(!selectors.getFocusTextBlock(snapshot) || !utils.isTextBlock(snapshot.context, event.block)),
38
+ actions: [
39
+ ({ event }) => [
40
+ execute({
41
+ type: "insert.block",
42
+ block: event.block,
43
+ placement: "auto",
44
+ select: "end"
45
+ })
46
+ ]
47
+ ]
48
+ }),
49
+ /**
50
+ * Fallback Behavior to avoid `insert.block` in case the Behaviors above all
51
+ * end up with a falsy guard.
52
+ */
53
+ defineBehavior({
54
+ on: "insert.block",
55
+ actions: []
56
+ }),
57
+ /**
58
+ * If multiple blocks are inserted, then the non-text blocks are filtered out
59
+ * and the text blocks are merged into one block
60
+ */
61
+ defineBehavior({
62
+ on: "insert.blocks",
63
+ guard: ({ snapshot, event }) => {
64
+ const textBlocks = event.blocks.filter(
65
+ (block) => utils.isTextBlock(snapshot.context, block)
66
+ );
67
+ return textBlocks.length === 0 ? !1 : textBlocks.reduce((targetBlock, incomingBlock) => utils.mergeTextBlocks({
68
+ context: snapshot.context,
69
+ targetBlock,
70
+ incomingBlock
71
+ }));
72
+ },
73
+ actions: [
74
+ // `insert.block` is raised so the Behavior above can handle the
75
+ // insertion
76
+ (_, block) => [raise({ type: "insert.block", block, placement: "auto" })]
77
+ ]
78
+ }),
79
+ /**
80
+ * Fallback Behavior to avoid `insert.blocks` in case the Behavior above
81
+ * ends up with a falsy guard.
82
+ */
83
+ defineBehavior({
84
+ on: "insert.blocks",
85
+ actions: []
86
+ })
87
+ ];
88
+ function OneLinePlugin() {
89
+ const editor = useEditor();
90
+ return useEffect(() => {
91
+ const unregisterBehaviors = oneLineBehaviors.map(
92
+ (behavior) => editor.registerBehavior({ behavior })
93
+ );
94
+ return () => {
95
+ for (const unregisterBehavior of unregisterBehaviors)
96
+ unregisterBehavior();
97
+ };
98
+ }, [editor]), null;
99
+ }
100
+ export {
101
+ OneLinePlugin
102
+ };
103
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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":";;;;;AAMA,MAAM,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA,EAKvB,eAAe;AAAA,IACb,IAAI;AAAA,IACJ,OAAO,CAAC,EAAC,SAAQ,MACf,SAAS,QAAQ,aAAa,UAAU,oBAAoB,QAAQ,IAChE,EAAC,WAAW,SAAS,QAAQ,cAC7B;AAAA,IACN,SAAS,CAAC,CAAC,GAAG,EAAC,gBAAe,CAAC,QAAQ,EAAC,MAAM,UAAU,IAAI,UAAU,CAAA,CAAC,CAAC;AAAA,EAAA,CACzE;AAAA;AAAA;AAAA;AAAA,EAID,eAAe;AAAA,IACb,IAAI;AAAA,IACJ,SAAS,CAAA;AAAA,EAAC,CACX;AAAA;AAAA;AAAA;AAAA,EAID,eAAe;AAAA,IACb,IAAI;AAAA,IACJ,OAAO,CAAC,EAAC,YACP,MAAM,cAAc,YAAY,MAAM,cAAc;AAAA,IACtD,SAAS,CAAA;AAAA,EAAC,CACX;AAAA;AAAA;AAAA;AAAA;AAAA,EAKD,eAAe;AAAA,IACb,IAAI;AAAA,IACJ,OAAO,CAAC,EAAC,UAAU,MAAA,MAIf,EAHqB,CAAA,UAA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AC,MAAM,CAAC,GAEJ;AACT;"}
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@portabletext/plugin-one-line",
3
+ "version": "0.0.0",
4
+ "description": "🤏 Restricts the Portable Text Editor to a single line",
5
+ "keywords": [
6
+ "portabletext",
7
+ "plugin",
8
+ "one-line",
9
+ "behaviors"
10
+ ],
11
+ "homepage": "https://portabletext.org",
12
+ "bugs": {
13
+ "url": "https://github.com/portabletext/plugins/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/portabletext/plugins.git",
18
+ "directory": "plugins/one-line"
19
+ },
20
+ "license": "MIT",
21
+ "author": "Sanity.io <hello@sanity.io>",
22
+ "sideEffects": false,
23
+ "type": "module",
24
+ "exports": {
25
+ ".": {
26
+ "source": "./src/index.ts",
27
+ "import": "./dist/index.js",
28
+ "require": "./dist/index.cjs",
29
+ "default": "./dist/index.js"
30
+ },
31
+ "./package.json": "./package.json"
32
+ },
33
+ "main": "./dist/index.cjs",
34
+ "module": "./dist/index.js",
35
+ "types": "./dist/index.d.ts",
36
+ "files": [
37
+ "src",
38
+ "dist"
39
+ ],
40
+ "devDependencies": {
41
+ "@portabletext/editor": "^1.48.2",
42
+ "@types/react": "^19.1.2",
43
+ "react": "^19.1.0"
44
+ },
45
+ "peerDependencies": {
46
+ "@portabletext/editor": "^1.48.2",
47
+ "react": "^19.1.0"
48
+ },
49
+ "scripts": {
50
+ "build": "pkg-utils build --strict --check --clean",
51
+ "check:lint": "biome lint .",
52
+ "check:react-compiler": "eslint --cache --no-inline-config --no-eslintrc --ignore-pattern '**/__tests__/**' --ext .cjs,.mjs,.js,.jsx,.ts,.tsx --parser @typescript-eslint/parser --plugin react-compiler --plugin react-hooks --rule 'react-compiler/react-compiler: [warn]' --rule 'react-hooks/rules-of-hooks: [error]' --rule 'react-hooks/exhaustive-deps: [error]' src",
53
+ "check:types": "tsc",
54
+ "check:types:watch": "tsc --watch",
55
+ "clean": "del .turbo && del dist && del node_modules",
56
+ "dev": "pkg-utils watch",
57
+ "lint:fix": "biome lint --write ."
58
+ }
59
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './plugin.one-line'
@@ -0,0 +1,136 @@
1
+ import {useEditor} from '@portabletext/editor'
2
+ import {defineBehavior, execute, raise} from '@portabletext/editor/behaviors'
3
+ import * as selectors from '@portabletext/editor/selectors'
4
+ import * as utils from '@portabletext/editor/utils'
5
+ import {useEffect} from 'react'
6
+
7
+ const oneLineBehaviors = [
8
+ /**
9
+ * Hitting Enter on an expanded selection should just delete that selection
10
+ * without causing a line break.
11
+ */
12
+ defineBehavior({
13
+ on: 'insert.break',
14
+ guard: ({snapshot}) =>
15
+ snapshot.context.selection && selectors.isSelectionExpanded(snapshot)
16
+ ? {selection: snapshot.context.selection}
17
+ : false,
18
+ actions: [(_, {selection}) => [execute({type: 'delete', at: selection})]],
19
+ }),
20
+ /**
21
+ * All other cases of `insert.break` should be aborted.
22
+ */
23
+ defineBehavior({
24
+ on: 'insert.break',
25
+ actions: [],
26
+ }),
27
+ /**
28
+ * `insert.block` `before` or `after` is not allowed in a one-line editor.
29
+ */
30
+ defineBehavior({
31
+ on: 'insert.block',
32
+ guard: ({event}) =>
33
+ event.placement === 'before' || event.placement === 'after',
34
+ actions: [],
35
+ }),
36
+ /**
37
+ * An ordinary `insert.block` is acceptable if it's a text block. In that
38
+ * case it will get merged into the existing text block.
39
+ */
40
+ defineBehavior({
41
+ on: 'insert.block',
42
+ guard: ({snapshot, event}) => {
43
+ const focusTextBlock = selectors.getFocusTextBlock(snapshot)
44
+
45
+ if (
46
+ !focusTextBlock ||
47
+ !utils.isTextBlock(snapshot.context, event.block)
48
+ ) {
49
+ return false
50
+ }
51
+
52
+ return true
53
+ },
54
+ actions: [
55
+ ({event}) => [
56
+ execute({
57
+ type: 'insert.block',
58
+ block: event.block,
59
+ placement: 'auto',
60
+ select: 'end',
61
+ }),
62
+ ],
63
+ ],
64
+ }),
65
+ /**
66
+ * Fallback Behavior to avoid `insert.block` in case the Behaviors above all
67
+ * end up with a falsy guard.
68
+ */
69
+ defineBehavior({
70
+ on: 'insert.block',
71
+ actions: [],
72
+ }),
73
+ /**
74
+ * If multiple blocks are inserted, then the non-text blocks are filtered out
75
+ * and the text blocks are merged into one block
76
+ */
77
+ defineBehavior({
78
+ on: 'insert.blocks',
79
+ guard: ({snapshot, event}) => {
80
+ const textBlocks = event.blocks.filter((block) =>
81
+ utils.isTextBlock(snapshot.context, block),
82
+ )
83
+
84
+ if (textBlocks.length === 0) {
85
+ return false
86
+ }
87
+
88
+ return textBlocks.reduce((targetBlock, incomingBlock) => {
89
+ return utils.mergeTextBlocks({
90
+ context: snapshot.context,
91
+ targetBlock,
92
+ incomingBlock,
93
+ })
94
+ })
95
+ },
96
+ actions: [
97
+ // `insert.block` is raised so the Behavior above can handle the
98
+ // insertion
99
+ (_, block) => [raise({type: 'insert.block', block, placement: 'auto'})],
100
+ ],
101
+ }),
102
+ /**
103
+ * Fallback Behavior to avoid `insert.blocks` in case the Behavior above
104
+ * ends up with a falsy guard.
105
+ */
106
+ defineBehavior({
107
+ on: 'insert.blocks',
108
+ actions: [],
109
+ }),
110
+ ]
111
+
112
+ /**
113
+ * @beta
114
+ * Restrict the editor to one line. The plugin takes care of blocking
115
+ * `insert.break` events and smart handling of other `insert.*` events.
116
+ *
117
+ * Place it with as high priority as possible to make sure other plugins don't
118
+ * overwrite `insert.*` events before this plugin gets a chance to do so.
119
+ */
120
+ export function OneLinePlugin() {
121
+ const editor = useEditor()
122
+
123
+ useEffect(() => {
124
+ const unregisterBehaviors = oneLineBehaviors.map((behavior) =>
125
+ editor.registerBehavior({behavior}),
126
+ )
127
+
128
+ return () => {
129
+ for (const unregisterBehavior of unregisterBehaviors) {
130
+ unregisterBehavior()
131
+ }
132
+ }
133
+ }, [editor])
134
+
135
+ return null
136
+ }