Skip to content

Commit

Permalink
Show mark toolbar for the deepest mark rather than the one first foun…
Browse files Browse the repository at this point in the history
…d in `fields.markdoc`/`fields.mdx` (#1233)
  • Loading branch information
emmatown committed Jul 18, 2024
1 parent b66a143 commit cfc1bdc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-parents-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Show mark toolbar for the deepest mark rather than the one first found in `fields.markdoc`/`fields.mdx`
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ const CustomMarkPopover: MarkPopoverRenderer = props => {
function getPopoverDecoration(state: EditorState): PopoverDecoration | null {
if (state.selection instanceof TextSelection) {
const schema = getEditorSchema(state.schema);
let decoration: PopoverDecoration | null = null;
for (const [name, componentConfig] of Object.entries(schema.components)) {
if (
componentConfig.kind !== 'mark' ||
Expand All @@ -537,14 +538,17 @@ function getPopoverDecoration(state: EditorState): PopoverDecoration | null {
aroundFrom.from === aroundTo?.from &&
aroundFrom.to === aroundTo.to
) {
return {
adaptToBoundary: 'flip',
kind: 'mark',
component: CustomMarkPopover,
mark: aroundFrom.mark,
from: aroundFrom.from,
to: aroundFrom.to,
};
const rangeSize = aroundFrom.to - aroundFrom.from;
if (!decoration || rangeSize < decoration.to - decoration.from) {
decoration = {
adaptToBoundary: 'flip',
kind: 'mark',
component: CustomMarkPopover,
mark: aroundFrom.mark,
from: aroundFrom.from,
to: aroundFrom.to,
};
}
}
}
if (schema.marks.link) {
Expand All @@ -558,16 +562,22 @@ function getPopoverDecoration(state: EditorState): PopoverDecoration | null {
linkAroundFrom.from === linkAroundTo?.from &&
linkAroundFrom.to === linkAroundTo.to
) {
return {
adaptToBoundary: 'flip',
kind: 'mark',
component: LinkPopover,
mark: linkAroundFrom.mark,
from: linkAroundFrom.from,
to: linkAroundFrom.to,
};
const rangeSize = linkAroundFrom.to - linkAroundFrom.from;
if (!decoration || rangeSize < decoration.to - decoration.from) {
return {
adaptToBoundary: 'flip',
kind: 'mark',
component: LinkPopover,
mark: linkAroundFrom.mark,
from: linkAroundFrom.from,
to: linkAroundFrom.to,
};
}
}
}
if (decoration) {
return decoration;
}
}

const editorSchema = getEditorSchema(state.schema);
Expand Down

0 comments on commit cfc1bdc

Please sign in to comment.