-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Textalign doesnt work as expected #1877
Comments
Please provide a codesandbox. |
Hi Philipp, not very experienced with codesandboxes to be honest. As described in my report, first I had to add the 'style' attribute to the Custom Paragraph to make it work (expected), but then for some reason when i try to change the alignment, a new css style is added. It works perfect initially, only after reloading (re-rendering the editor) it doesnt work anymore. |
Do you use the TextAlign extension? |
Yes, basically thats what I do:
Then my CustomParagraph (to have classes):
and in the MenuBar I call:
On initial load it works great, only when I reload the text the style attributes are gone (that's why I then added the style attribute, but that is apparently not the solution). For some reason the editor doesnt want to render the "textAlign" attribute. |
Well, I set up a sandbox for you, it show's the same bug: https://codesandbox.io/s/tiptap-issue-template-forked-0rwv3?file=/src/components/Tiptap.vue The result is actually interesting, because I can see on the website that with vue 3 seems to be fine? |
I kept debugging the parseHTML and renderHTMl functions and I noticed, that while the parseHTML function delivers the correct value, e.g. 'left', 'center', 'right' or 'justify (or 'left' in case there is nothing), the renderHTML function always delivered:
So I came to the point where I thought there must something with the value returned by the parseHTML function. So I copied the TextAlign Extension created a new one "CustomTextAlign.js" and this seems to work: import { Extension } from '@tiptap/core';
const TextAlign = Extension.create({
name: 'textAlign',
defaultOptions: {
types: [],
alignments: ['left', 'center', 'right', 'justify'],
defaultAlignment: 'left',
},
addGlobalAttributes() {
return [
{
types: this.options.types,
attributes: {
textAlign: {
default: this.options.defaultAlignment,
//parseHTML: element => element.style.textAlign || this.options.defaultAlignment,
parseHTML: element => {
return {
textAlign: element.style.textAlign || this.options.defaultAlignment,
};
},
renderHTML: attributes => {
if (attributes.textAlign === this.options.defaultAlignment) {
return {};
}
return { style: `text-align: ${attributes.textAlign}` };
},
},
},
},
];
},
addCommands() {
return {
setTextAlign: (alignment) => ({ commands }) => {
if (!this.options.alignments.includes(alignment)) {
return false;
}
return this.options.types.every(type => commands.updateAttributes(type, { textAlign: alignment }));
},
unsetTextAlign: () => ({ commands }) => {
return this.options.types.every(type => commands.resetAttributes(type, 'textAlign'));
},
};
},
addKeyboardShortcuts() {
return {
'Mod-Shift-l': () => this.editor.commands.setTextAlign('left'),
'Mod-Shift-e': () => this.editor.commands.setTextAlign('center'),
'Mod-Shift-r': () => this.editor.commands.setTextAlign('right'),
'Mod-Shift-j': () => this.editor.commands.setTextAlign('justify'),
};
},
});
export { TextAlign, TextAlign as default };
//# sourceMappingURL=tiptap-extension-text-align.esm.js.map To make long story short, instead of returning a String, we return an object: attributes: {
textAlign: {
default: this.options.defaultAlignment,
//parseHTML: element => element.style.textAlign || this.options.defaultAlignment,
parseHTML: element => {
return {
textAlign: element.style.textAlign || this.options.defaultAlignment,
};
},
...
}, It now it works as expected (not tested, of course). So, if you think that's a solution - please explain why it works Philipp. ;-) |
Hey, the solution is simple: just update all of your tiptap dependencies to the latest versions. Because of this change. Update and everything should work fine :) |
Oh no, so simple...! But still, I found a solution, not too bad? |
Haha yeah, you found the old syntax :D |
Description
Working on Vue 2.
I have a Custom Paragraph, like:
I initially noticed that when I don't add the style attribute Tiptap (Prosemirror) will throw away my text alignment on initial render.
Now, the text alignment stays after rendering, but it is not possible anymore to change the alignment. I noticed while debugging that while the text-alignment changes, it doesn't do anything in the editor.
Also, it is possible to change to center, right and justify but not back to left.
The same is true for Headings and Images.
Steps to reproduce the bug
Steps to reproduce the behavior:
If you come back to the text, the node looks like:
If you then try to align, e.g. to the right, it becomes:
Expected behavior
Quite obvious, after a text alignment is set, should be possible after re-rendering the text to change/adjust it.
The text was updated successfully, but these errors were encountered: