Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fix-ime-cursor-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tiptap/extension-text-style': patch
'@tiptap/core': patch
---

fix: prevent cursor jump during IME composition in colored text
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function injectExtensionAttributesToParseRule(
? item.attribute.parseHTML(node)
: fromString(node.getAttribute(item.name))

if (value === null || value === undefined) {
if (value === null || value === undefined || value === '') {
return items
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const BackgroundColor = Extension.create<BackgroundColorOptions>({
}
}

return element.style.backgroundColor?.replace(/['"]+/g, '')
return element.style.backgroundColor?.replace(/['"]+/g, '') || null
},
renderHTML: attributes => {
if (!attributes.backgroundColor) {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-text-style/src/color/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const Color = Extension.create<ColorOptions>({
}
}

return element.style.color?.replace(/['"]+/g, '')
return element.style.color?.replace(/['"]+/g, '') || null
},
renderHTML: attributes => {
if (!attributes.color) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const FontFamily = Extension.create<FontFamilyOptions>({
attributes: {
fontFamily: {
default: null,
parseHTML: element => element.style.fontFamily,
parseHTML: element => element.style.fontFamily || null,
renderHTML: attributes => {
if (!attributes.fontFamily) {
return {}
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-text-style/src/font-size/font-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const FontSize = Extension.create<FontSizeOptions>({
attributes: {
fontSize: {
default: null,
parseHTML: element => element.style.fontSize,
parseHTML: element => element.style.fontSize || null,
renderHTML: attributes => {
if (!attributes.fontSize) {
return {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const LineHeight = Extension.create<LineHeightOptions>({
attributes: {
lineHeight: {
default: null,
parseHTML: element => element.style.lineHeight,
parseHTML: element => element.style.lineHeight || null,
renderHTML: attributes => {
if (!attributes.lineHeight) {
return {}
Expand Down
20 changes: 20 additions & 0 deletions packages/extension-text-style/src/text-style/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ export const TextStyle = Mark.create<TextStyleOptions>({
return ['span', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},

addMarkView() {
return ({ HTMLAttributes }) => {
const span = document.createElement('span')

// Use setAttribute instead of style.cssText to preserve original
// color format (e.g. hex #FF0000) and prevent browser normalization
// to rgb() which causes mark attribute mismatches during IME composition.
Object.entries(HTMLAttributes).forEach(([attr, value]) => {
if (value != null && value !== false) {
span.setAttribute(attr, String(value))
}
})

return {
dom: span,
contentDOM: span,
}
}
},

addCommands() {
return {
toggleTextStyle:
Expand Down