Skip to content
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

Make Quill instances detachable and reconfigurable #4402

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

singintime
Copy link

Motivation

This PR addresses discussion #4397 and issue #4234, providing a way to apply a new configuration to an existing Quill editor. Please refer to those for further information about the reasons behind this change.

Proposed changes

  • Adding a Subscriber class, acting as a proxy to the EventTarget API and keeping track of all subscriptions to event listeners originated by Quill components and modules,
  • Adding getters and setters to bind Subscriber instances to Quill root elements. Subscriber instances are stored in a WeakMap, similar to what happens for Quill instances.
  • Using the subscriber to replace all non-static calls of addEventListener(),
  • Unsubscribing from all event listeners originated by blots when they detach(),
  • Unsubscribing from all events originated by a Quill instance when detached or reconfigured,
  • Adding a detach() cleanup method to Quill instances,
  • Adding a configure() method to apply a new configuration to an already existing instance of Quill,
  • Providing unit test coverage for the above-mentioned changes.

packages/quill/src/core/subscriber.ts Outdated Show resolved Hide resolved
packages/quill/src/core/quill.ts Show resolved Hide resolved
this.emitter.removeAllListeners();
this.emitter.ignoreDOM();
this.scroll.detach();
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong but this method does not appear to fully tear down passed in options, modules, etc. It seems to me that this method should return the quill instance to "factory settings" no?

Additionally, any dynamically changed things (like calling quill.keyboard.addBinding) should also be cleaned up

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is not supposed to restore the quill instance to "factory settings", that would be the job of configure() called without arguments. detach() makes the Quill instance unresponsive to all events and user actions.

This is also why I didn't reset dynamically changed modules (keyboard, clipboard, history, etc.): a detached Quill instance is "dead", and not meant to be used anymore until reconfigured via the configure() method

packages/website/content/docs/api.mdx Outdated Show resolved Hide resolved
packages/website/content/docs/api.mdx Outdated Show resolved Hide resolved
packages/website/content/docs/api.mdx Outdated Show resolved Hide resolved
packages/quill/src/core/subscriber.ts Outdated Show resolved Hide resolved
@@ -234,6 +235,7 @@ class Quill {
}
const scrollBlotName = Parchment.ScrollBlot.blotName;
const ScrollBlot = this.options.registry.query(scrollBlotName);
console.log(ScrollBlot);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗑️ ?

@@ -27,6 +28,14 @@ class Theme {
protected options: ThemeOptions,
) {}

detach() {
Object.values(this.modules).forEach((module) => {
if (module instanceof Module) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if instead a better check might be if ('detach' in module). You can register things with quill that aren't literally subclasses of Module

@singintime singintime closed this Sep 13, 2024
@singintime singintime reopened this Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants