Skip to content

Commit

Permalink
fix(markdown behavior): ignore spans in auto blockquote/heading/llist…
Browse files Browse the repository at this point in the history
… behaviors
  • Loading branch information
christianhg committed Nov 25, 2024
1 parent 29a2c95 commit a1cd1ee
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
65 changes: 59 additions & 6 deletions packages/editor/src/editor/behavior/behavior.markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
getFocusBlock,
getFocusSpan,
getFocusTextBlock,
getTextBlockText,
selectionIsCollapsed,
} from './behavior.utils'
import {spanSelectionPointToBlockOffset} from './behavior.utils.block-offset'

/**
* @alpha
Expand Down Expand Up @@ -55,8 +57,25 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
return false
}

const caretAtTheEndOfQuote = context.selection.focus.offset === 1
const looksLikeMarkdownQuote = /^>/.test(focusSpan.node.text)
const blockOffset = spanSelectionPointToBlockOffset({
value: context.value,
selectionPoint: {
path: [
{_key: focusTextBlock.node._key},
'children',
{_key: focusSpan.node._key},
],
offset: context.selection.focus.offset,
},
})

if (!blockOffset) {
return false
}

const blockText = getTextBlockText(focusTextBlock.node)
const caretAtTheEndOfQuote = blockOffset.offset === 1
const looksLikeMarkdownQuote = /^>/.test(blockText)
const blockquoteStyle = config.blockquoteStyle?.({schema: context.schema})

if (
Expand Down Expand Up @@ -227,11 +246,28 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
return false
}

const markdownHeadingSearch = /^#+/.exec(focusSpan.node.text)
const blockOffset = spanSelectionPointToBlockOffset({
value: context.value,
selectionPoint: {
path: [
{_key: focusTextBlock.node._key},
'children',
{_key: focusSpan.node._key},
],
offset: context.selection.focus.offset,
},
})

if (!blockOffset) {
return false
}

const blockText = getTextBlockText(focusTextBlock.node)
const markdownHeadingSearch = /^#+/.exec(blockText)
const level = markdownHeadingSearch
? markdownHeadingSearch[0].length
: undefined
const caretAtTheEndOfHeading = context.selection.focus.offset === level
const caretAtTheEndOfHeading = blockOffset.offset === level

if (!caretAtTheEndOfHeading) {
return false
Expand Down Expand Up @@ -338,12 +374,29 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
return false
}

const blockOffset = spanSelectionPointToBlockOffset({
value: context.value,
selectionPoint: {
path: [
{_key: focusTextBlock.node._key},
'children',
{_key: focusSpan.node._key},
],
offset: context.selection.focus.offset,
},
})

if (!blockOffset) {
return false
}

const blockText = getTextBlockText(focusTextBlock.node)
const defaultStyle = config.defaultStyle?.({schema: context.schema})
const looksLikeUnorderedList = /^(-|\*)/.test(focusSpan.node.text)
const looksLikeUnorderedList = /^(-|\*)/.test(blockText)
const unorderedListStyle = config.unorderedListStyle?.({
schema: context.schema,
})
const caretAtTheEndOfUnorderedList = context.selection.focus.offset === 1
const caretAtTheEndOfUnorderedList = blockOffset.offset === 1

if (
defaultStyle &&
Expand Down
38 changes: 38 additions & 0 deletions packages/editor/src/editor/behavior/behavior.utils.block-offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,41 @@ export function blockOffsetToSpanSelectionPoint({

return selectionPoint
}

export function spanSelectionPointToBlockOffset({
value,
selectionPoint,
}: {
value: Array<PortableTextBlock>
selectionPoint: {
path: [KeyedSegment, 'children', KeyedSegment]
offset: number
}
}): BlockOffset | undefined {
let offset = 0

for (const block of value) {
if (block._key !== selectionPoint.path[0]._key) {
continue
}

if (!isPortableTextTextBlock(block)) {
continue
}

for (const child of block.children) {
if (!isPortableTextSpan(child)) {
continue
}

if (child._key === selectionPoint.path[2]._key) {
return {
path: [{_key: block._key}],
offset: offset + selectionPoint.offset,
}
}

offset += child.text.length
}
}
}
6 changes: 5 additions & 1 deletion packages/editor/src/editor/behavior/behavior.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ export function isEmptyTextBlock(block: PortableTextBlock) {
}

const onlyText = block.children.every(isPortableTextSpan)
const blockText = block.children.map((child) => child.text ?? '').join('')
const blockText = getTextBlockText(block)

return onlyText && blockText === ''
}

export function getTextBlockText(block: PortableTextTextBlock) {
return block.children.map((child) => child.text ?? '').join('')
}

0 comments on commit a1cd1ee

Please sign in to comment.