@portabletext/editor 1.9.0 → 1.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -6,7 +6,7 @@ import {getFocusSpan, selectionIsCollapsed} from './behavior.utils'
6
6
  * @alpha
7
7
  */
8
8
  export type LinkBehaviorsConfig = {
9
- mapLinkAnnotation?: (config: {
9
+ linkAnnotation?: (context: {
10
10
  schema: PortableTextMemberSchemaTypes
11
11
  url: string
12
12
  }) => {name: string; value: {[prop: string]: unknown}} | undefined
@@ -24,7 +24,7 @@ export function createLinkBehaviors(config: LinkBehaviorsConfig) {
24
24
  const url = looksLikeUrl(text) ? text : undefined
25
25
  const annotation =
26
26
  url !== undefined
27
- ? config.mapLinkAnnotation?.({url, schema: context.schema})
27
+ ? config.linkAnnotation?.({url, schema: context.schema})
28
28
  : undefined
29
29
 
30
30
  if (annotation && !selectionCollapsed) {
@@ -56,7 +56,7 @@ export function createLinkBehaviors(config: LinkBehaviorsConfig) {
56
56
  const url = looksLikeUrl(text) ? text : undefined
57
57
  const annotation =
58
58
  url !== undefined
59
- ? config.mapLinkAnnotation?.({url, schema: context.schema})
59
+ ? config.linkAnnotation?.({url, schema: context.schema})
60
60
  : undefined
61
61
 
62
62
  if (url && annotation && selectionCollapsed) {
@@ -11,25 +11,25 @@ import {
11
11
  * @alpha
12
12
  */
13
13
  export type MarkdownBehaviorsConfig = {
14
- mapBreakObject?: (
15
- schema: PortableTextMemberSchemaTypes,
16
- ) => {name: string; value?: {[prop: string]: unknown}} | undefined
17
- mapDefaultStyle?: (
18
- schema: PortableTextMemberSchemaTypes,
19
- ) => string | undefined
20
- mapHeadingStyle?: (
21
- schema: PortableTextMemberSchemaTypes,
22
- level: number,
23
- ) => string | undefined
24
- mapBlockquoteStyle?: (
25
- schema: PortableTextMemberSchemaTypes,
26
- ) => string | undefined
27
- mapUnorderedListStyle?: (
28
- schema: PortableTextMemberSchemaTypes,
29
- ) => string | undefined
30
- mapOrderedListStyle?: (
31
- schema: PortableTextMemberSchemaTypes,
32
- ) => string | undefined
14
+ horizontalRuleObject?: (context: {
15
+ schema: PortableTextMemberSchemaTypes
16
+ }) => {name: string; value?: {[prop: string]: unknown}} | undefined
17
+ defaultStyle?: (context: {
18
+ schema: PortableTextMemberSchemaTypes
19
+ }) => string | undefined
20
+ headingStyle?: (context: {
21
+ schema: PortableTextMemberSchemaTypes
22
+ level: number
23
+ }) => string | undefined
24
+ blockquoteStyle?: (context: {
25
+ schema: PortableTextMemberSchemaTypes
26
+ }) => string | undefined
27
+ unorderedListStyle?: (context: {
28
+ schema: PortableTextMemberSchemaTypes
29
+ }) => string | undefined
30
+ orderedListStyle?: (context: {
31
+ schema: PortableTextMemberSchemaTypes
32
+ }) => string | undefined
33
33
  }
34
34
 
35
35
  /**
@@ -55,7 +55,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
55
55
 
56
56
  const caretAtTheEndOfQuote = context.selection.focus.offset === 1
57
57
  const looksLikeMarkdownQuote = /^>/.test(focusSpan.node.text)
58
- const blockquoteStyle = config.mapBlockquoteStyle?.(context.schema)
58
+ const blockquoteStyle = config.blockquoteStyle?.({schema: context.schema})
59
59
 
60
60
  if (
61
61
  caretAtTheEndOfQuote &&
@@ -104,13 +104,22 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
104
104
  const automaticBreak = defineBehavior({
105
105
  on: 'insert text',
106
106
  guard: ({context, event}) => {
107
- const isDash = event.text === '-'
108
-
109
- if (!isDash) {
107
+ const hrCharacter =
108
+ event.text === '-'
109
+ ? '-'
110
+ : event.text === '*'
111
+ ? '*'
112
+ : event.text === '_'
113
+ ? '_'
114
+ : undefined
115
+
116
+ if (hrCharacter === undefined) {
110
117
  return false
111
118
  }
112
119
 
113
- const breakObject = config.mapBreakObject?.(context.schema)
120
+ const breakObject = config.horizontalRuleObject?.({
121
+ schema: context.schema,
122
+ })
114
123
  const focusBlock = getFocusTextBlock(context)
115
124
  const selectionCollapsed = selectionIsCollapsed(context)
116
125
 
@@ -123,17 +132,17 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
123
132
  .map((child) => child.text ?? '')
124
133
  .join('')
125
134
 
126
- if (onlyText && blockText === '--') {
127
- return {breakObject, focusBlock}
135
+ if (onlyText && blockText === `${hrCharacter}${hrCharacter}`) {
136
+ return {breakObject, focusBlock, hrCharacter}
128
137
  }
129
138
 
130
139
  return false
131
140
  },
132
141
  actions: [
133
- () => [
142
+ (_, {hrCharacter}) => [
134
143
  {
135
144
  type: 'insert text',
136
- text: '-',
145
+ text: hrCharacter,
137
146
  },
138
147
  ],
139
148
  (_, {breakObject, focusBlock}) => [
@@ -179,27 +188,26 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
179
188
  }
180
189
 
181
190
  const markdownHeadingSearch = /^#+/.exec(focusSpan.node.text)
182
- const headingLevel = markdownHeadingSearch
191
+ const level = markdownHeadingSearch
183
192
  ? markdownHeadingSearch[0].length
184
193
  : undefined
185
- const caretAtTheEndOfHeading =
186
- context.selection.focus.offset === headingLevel
194
+ const caretAtTheEndOfHeading = context.selection.focus.offset === level
187
195
 
188
196
  if (!caretAtTheEndOfHeading) {
189
197
  return false
190
198
  }
191
199
 
192
- const headingStyle =
193
- headingLevel !== undefined
194
- ? config.mapHeadingStyle?.(context.schema, headingLevel)
200
+ const style =
201
+ level !== undefined
202
+ ? config.headingStyle?.({schema: context.schema, level})
195
203
  : undefined
196
204
 
197
- if (headingLevel !== undefined && headingStyle !== undefined) {
205
+ if (level !== undefined && style !== undefined) {
198
206
  return {
199
207
  focusTextBlock,
200
208
  focusSpan,
201
- style: headingStyle,
202
- level: headingLevel,
209
+ style: style,
210
+ level,
203
211
  }
204
212
  }
205
213
 
@@ -254,7 +262,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
254
262
  focusTextBlock.node.children[0]._key === focusSpan.node._key &&
255
263
  context.selection.focus.offset === 0
256
264
 
257
- const defaultStyle = config.mapDefaultStyle?.(context.schema)
265
+ const defaultStyle = config.defaultStyle?.({schema: context.schema})
258
266
 
259
267
  if (
260
268
  atTheBeginningOfBLock &&
@@ -293,9 +301,11 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
293
301
  return false
294
302
  }
295
303
 
296
- const defaultStyle = config.mapDefaultStyle?.(context.schema)
304
+ const defaultStyle = config.defaultStyle?.({schema: context.schema})
297
305
  const looksLikeUnorderedList = /^(-|\*)/.test(focusSpan.node.text)
298
- const unorderedListStyle = config.mapUnorderedListStyle?.(context.schema)
306
+ const unorderedListStyle = config.unorderedListStyle?.({
307
+ schema: context.schema,
308
+ })
299
309
  const caretAtTheEndOfUnorderedList = context.selection.focus.offset === 1
300
310
 
301
311
  if (
@@ -314,7 +324,9 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
314
324
  }
315
325
 
316
326
  const looksLikeOrderedList = /^1./.test(focusSpan.node.text)
317
- const orderedListStyle = config.mapOrderedListStyle?.(context.schema)
327
+ const orderedListStyle = config.orderedListStyle?.({
328
+ schema: context.schema,
329
+ })
318
330
  const caretAtTheEndOfOrderedList = context.selection.focus.offset === 2
319
331
 
320
332
  if (