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

💡 Optional mutation observer for block tools #2205

Closed
medzhidov opened this issue Dec 4, 2022 · 5 comments · Fixed by #2548
Closed

💡 Optional mutation observer for block tools #2205

medzhidov opened this issue Dec 4, 2022 · 5 comments · Fixed by #2548

Comments

@medzhidov
Copy link

Sometimes it needs to stop mutation observer on blocks, because they are not created to be observed. For example, i create an custom tool block that has several logic, attributes/classes/styles and i want to save only content that i dispatched via block.dispatchChange();, because mutation observer trigger "didMutated" when content is not really edited (for example trigger class on image by hovering).

I already solved this by edited code, so there is my suggested solution:

// src/components/tools/base.ts:80 - add
/**
   * Is mutation observer enabled for Tool
   */
  IsEnabledMutationObserver = 'isEnabledMutationObserver',
// src/components/tools/block.ts:70 - add
/**
   * Returns true if Tool supports linebreaks
   */
  public get isEnabledMutationObserver(): boolean {
    return this.constructable[InternalBlockToolSettings.IsEnabledMutationObserver] !== false;
  }
//src/components/block/index.ts:267 - replace
    if (tool.isEnabledMutationObserver) {
      this.mutationObserver = new MutationObserver(this.didMutated);
    }

//src/components/block/index.ts:295 - replace
this.mutationObserver?.observe(

//src/components/block/index.ts:716 - replace
this.mutationObserver?.disconnect();

So now we can add in our block tool a static method and mutation will be ignored for this block:

export default class SimpleImage {
   constructor(){...}
   
   static get isEnabledMutationObserver() {
        return false;
   }
   
   render() {...}

It's 2 am so i may forget something but it works perfectly in my project now.

@Bobsans
Copy link

Bobsans commented Mar 18, 2023

I have related problem too. I write tool with map and map script update block class many times per second. So if i call block.dispatchChange() from tool - debouncer ignore this call, and data saved only if block lose focus (observer unsubscribes). Please add possibility to disable observer for tool

@lucas-varela
Copy link

@medzhidov and @Bobsans, I've implemented a way to disable the mutation observation, check out this PR: #2317 

Also, I've created a branch that includes the dist folder built with these changes: https://github.com/lucas-varela/editor.js/tree/build/optional-mutation-observer

This branch can be used on an npm install or yarn add, like:

npm install https://github.com/lucas-varela/editor.js.git#build/optional-mutation-observer

You can test it if you'd like, but I don't recommend using it in production, for obvious reasons. Either fork my feature branch and build it, or wait for my PR to (hopefully) be merged.

@medzhidov
Copy link
Author

medzhidov commented Sep 2, 2023

@lucas-varela Okay, but current realisation is a bad: i have a vue component with a LOT of divs inside, so now i must add this attribute to EACH element. Why you just cant see for parent blocks with that attribute like this?
CleanShot 2023-09-02 at 17 32 51

@neSpecc
Copy link
Member

neSpecc commented Sep 2, 2023

@medzhidov

Why you just cant see for parent blocks with that attribute like this?

You're right, it would be better to improve it like that.

@medzhidov
Copy link
Author

@neSpecc Created PR for this fix, added "true" as an value of attribute to make it more variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants