Git Product home page Git Product logo

Comments (2)

azvoncov-smartling avatar azvoncov-smartling commented on May 23, 2024

Also, if you apply a change of direction to the selected text close to the Void Element - Chrome removes the span nodes and after any changes (Ctrl+z for example) the editor crashes

output1.mov

from slate.

azvoncov-smartling avatar azvoncov-smartling commented on May 23, 2024

btw, I have a raw version of the hook that fixes this problem, you can also save a new direction locally and change it in the editor if desired

const config = { attributes: true, childList: true, subtree: true };

function isElementNode(node: Node | null): node is Element {
    return !!node && node.nodeType === Node.ELEMENT_NODE;
}

export function useChangingDirection(editor: SlateEditor) {
    useLayoutEffect(() => {
        const textbox = ReactEditor.toDOMNode(editor, editor);

        const callback: MutationCallback = (mutationsList) => {
            const changedDirection = mutationsList.some(mutation => {
                const { type, attributeName, target } = mutation;

                if (type === "attributes" && target.nodeName === "DIV") {
                    if (attributeName === "style"
                        && isElementNode(target)
                    ) {
                        const styleAttribute = target.getAttribute("style");

                        return styleAttribute && styleAttribute.includes("direction");
                    }

                    return attributeName === "dir";
                }

                return false;
            });

            if (changedDirection) {
                const childListMutations = mutationsList
                    .filter(mutation => mutation.type === "childList")
                    .reverse();

                childListMutations.forEach(currentMutation => {
                    currentMutation.addedNodes.forEach(node => {
                        node.parentNode?.removeChild(node);
                    });

                    currentMutation.removedNodes.forEach(node => {
                        currentMutation.target.insertBefore(node, currentMutation.nextSibling);
                    });
                });
            }
        };

        const observer = new MutationObserver(callback);
        observer.observe(textbox, config);

        return () => observer.disconnect();
    }, [editor]);
}

from slate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.