-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Format toggled off after entering a newline #4306
Comments
If it helps, I'm experimenting with working around this by overriding the handler for the "enter" key using pretty much the same code as the default but with a few tweaks (see EDIT: BAD CODE! See the real workaround belowbindings: { handleEnter: { key: "Enter", handler: (range, context) => { const lineFormats = Object.keys(context.format).reduce( (formats, format) => { if ( // NOTE: original implementation had Scope.BLOCK here. So it looks like it is // deliberately resetting inline styles on newlines, for some reason. this._quill.scroll.query(format, Scope.ANY) && !Array.isArray(context.format[format]) ) { formats[format] = context.format[format]; } return formats; }, {}, ); |
OK, the "workaround" I shared above doesn't actually make much sense after a deeper look. Looks like I did some digging and eventually arrived at #3428, where the "preserve styles on newline" behavior was deliberately dropped with the intention to make the behavior more intuitive. I wonder if there's a chance we could reconsider the default behavior? I think folks in that PR made some good points re. which is the most intuitive behavior, but even leaving that aside: this feels like a relatively important breaking change. Might at least be worth adding a note to the "keyboard" section of the migration notes? For anyone interested, below is the overloaded handler that reinstates that behavior. I'll try later to see if there's a more elegant way to "decorate" the default handler rather than redefining it completely, but the essence of the fix is here for those who prefer the old behavior. bindings: {
handleEnter: {
key: "Enter",
handler: (range, context) => {
const lineFormats = Object.keys(context.format).reduce(
(formats, format) => {
if (
this._quill.scroll.query(format, Scope.BLOCK) &&
!Array.isArray(context.format[format])
) {
formats[format] = context.format[format];
}
return formats;
},
{},
);
const delta = new Delta()
.retain(range.index)
.delete(range.length)
.insert("\n", lineFormats);
this._quill.updateContents(delta, Quill.sources.USER);
this._quill.setSelection(range.index + 1, Quill.sources.SILENT);
// NOTE: Changed from default handler!
// Applies previous formats on the new line. This was dropped in
// https://github.com/slab/quill/commit/ba5461634caa8e24641b687f2d1a8768abfec640
Object.keys(context.format).forEach((name) => {
if (lineFormats[name] != null) return;
if (Array.isArray(context.format[name])) return;
if (name === "code" || name === "link") return;
this._quill.format(
name,
context.format[name],
Quill.sources.USER,
);
});
// NOTE: Changed from default handler!
// Ensure toolbar reflects the styles after the newline. Otherwise the buttons won't reflect
// the active formats until the user starts typing.
// This is probably related to https://github.com/slab/quill/issues/4305.
this._quill.getModule("toolbar").update(range);
this._quill.focus();
},
},
} |
@juanedi This is great. Just curious about something though, I tried adding this to my code and I'm getting some confusing results...
Is there something I'm missing – any trick to how you're setting things up where Here's my config currently (note:
EDIT I was able to get rid of any errors by replacing |
Hey @jonathaneckmier!
In the context where I took that code excerpt from, There shouldn't be anything wrong with the way you adapted the code for your use case.
You're good! In my case, the editor is initialized within a custom element that has a A more context-agnostic way to define it would be to:
I would 100% recommend you don't keep going after seeing an error like that 😅 Good luck! |
@juanedi Thanks so much for the reply, this is really helpful additional context, so thank you! Hopefully if others are also looking at this issue this thread will be helpful. I wish there was a configuration in Quill to retain formatting across line breaks. |
yep, this thread just helped me fixing this very strange behaviour of the Quill editor. |
If I'm writing with formats like "bold" and "inline" enabled and press a line break with the enter key, the formats get toggled off (so if I keep typing on the next line my text won't have them).
Steps for Reproduction
Expected behavior:
Whatever you type in the second line preserves the formatting toggles you had before.
Actual behavior:
Formats get cleared after the line break.
Platforms:
Tested this in macos using Chrome, Firefox and Safari.
Version:
2.0.2
This was working in my application on 1.3.7. Oddly, I also see the bug in https://v1.quilljs.com, so am not entirely sure if this was caused entirely by the version upgrade or if there is another factor at play here.
The text was updated successfully, but these errors were encountered: