@portabletext/plugin-one-line 3.0.23 → 3.0.25

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/plugin-one-line",
3
- "version": "3.0.23",
3
+ "version": "3.0.25",
4
4
  "description": "🤏 Restricts the Portable Text Editor to a single line",
5
5
  "keywords": [
6
6
  "portabletext",
@@ -31,7 +31,6 @@
31
31
  "main": "./dist/index.js",
32
32
  "types": "./dist/index.d.ts",
33
33
  "files": [
34
- "src",
35
34
  "dist"
36
35
  ],
37
36
  "dependencies": {
@@ -46,11 +45,11 @@
46
45
  "react": "^19.2.1",
47
46
  "typescript": "5.9.3",
48
47
  "typescript-eslint": "^8.48.0",
49
- "@portabletext/editor": "^3.3.3"
48
+ "@portabletext/editor": "^3.3.5"
50
49
  },
51
50
  "peerDependencies": {
52
51
  "react": "^18.3 || ^19",
53
- "@portabletext/editor": "^3.3.3"
52
+ "@portabletext/editor": "^3.3.5"
54
53
  },
55
54
  "engines": {
56
55
  "node": ">=20.19 <22 || >=22.12"
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './plugin.one-line'
@@ -1,145 +0,0 @@
1
- import {useEditor} from '@portabletext/editor'
2
- import {defineBehavior, 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}) => [raise({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 event.placement !== 'auto' || event.select !== 'end'
53
- },
54
- actions: [
55
- ({event}) => [
56
- raise({
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
- 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
- },
80
- actions: [],
81
- }),
82
- /**
83
- * If multiple blocks are inserted, then the non-text blocks are filtered out
84
- * and the text blocks are merged into one block
85
- */
86
- defineBehavior({
87
- on: 'insert.blocks',
88
- guard: ({snapshot, event}) => {
89
- const textBlocks = event.blocks.filter((block) =>
90
- utils.isTextBlock(snapshot.context, block),
91
- )
92
-
93
- if (textBlocks.length === 0) {
94
- return false
95
- }
96
-
97
- return textBlocks.reduce((targetBlock, incomingBlock) => {
98
- return utils.mergeTextBlocks({
99
- context: snapshot.context,
100
- targetBlock,
101
- incomingBlock,
102
- })
103
- })
104
- },
105
- actions: [
106
- // `insert.block` is raised so the Behavior above can handle the
107
- // insertion
108
- (_, block) => [raise({type: 'insert.block', block, placement: 'auto'})],
109
- ],
110
- }),
111
- /**
112
- * Fallback Behavior to avoid `insert.blocks` in case the Behavior above
113
- * ends up with a falsy guard.
114
- */
115
- defineBehavior({
116
- on: 'insert.blocks',
117
- actions: [],
118
- }),
119
- ]
120
-
121
- /**
122
- * @public
123
- * Restrict the editor to one line. The plugin takes care of blocking
124
- * `insert.break` events and smart handling of other `insert.*` events.
125
- *
126
- * Place it with as high priority as possible to make sure other plugins don't
127
- * overwrite `insert.*` events before this plugin gets a chance to do so.
128
- */
129
- export function OneLinePlugin() {
130
- const editor = useEditor()
131
-
132
- useEffect(() => {
133
- const unregisterBehaviors = oneLineBehaviors.map((behavior) =>
134
- editor.registerBehavior({behavior}),
135
- )
136
-
137
- return () => {
138
- for (const unregisterBehavior of unregisterBehaviors) {
139
- unregisterBehavior()
140
- }
141
- }
142
- }, [editor])
143
-
144
- return null
145
- }