Code Monkey home page Code Monkey logo

Comments (9)

jhchen avatar jhchen commented on August 23, 2024

What's the use case?

from delta.

 avatar commented on August 23, 2024

The use case is the following. When someone adds an image via a toolbar button, it shows a window where the user can pick an image from its hard drive. When the user selects one or more images. The editor first inserts a div (BlockEmbed), with the filename as text and a unique sequence identifier as dataset.

<div class="editor-uploading" data-sequence="123421">IMG_1.jpeg</div>

This div corresponds to a certain delta like:

message: {"sequence":123421, text:"IMG_1.jpeg"}

Afther this div is added, the image or images are being uploaded to the server including the sequence number. When the upload is finished, the server responds with the actual url and the sequence number. The image should now be shown to the user at the position of the div with that sequence number. So the div should be replaced with:

<img src="http://some_image.jpg" class="editor-uploaded" alt="some text" />

This img-tag corresponds with the following delta:

image: {src: "http://some_image.jpg", alt="some text", "sequence":123421}

Somehow I need to modify the delta of the complete text and delete the div and replace it with an image tag. How can I accomplish this?

Currently I have the following solution:

replaceSequenceWithImage(image) {
    let delta = quill.getContents();
    let range = quill.getSelection(true);

    for (let i = 0; i < delta.ops.length; i++) {
        let insert = delta.ops[i].insert;
        if (insert.hasOwnProperty('message') && insert.message.sequence == image.sequence) {
            delete insert.message;
            insert.image = image;
        }
    }
    quill.setContents(delta, Quill.sources.USER);
    quill.setSelection(range, Quill.sources.USER);
}

This solution feels bad, because at every update I use setContents while a better solution would be to use updateContents. But I don't know how to get to the index for retain().

from delta.

jhchen avatar jhchen commented on August 23, 2024

I think this is the wrong layer of abstraction for what you are trying to accomplish, which is why best practice is to not generate Deltas yourself.

It sounds like you are using Quill and Parchment and it would be less error prone to have blockembed replace itself with the real image blot instead.

from delta.

 avatar commented on August 23, 2024

Yes, I am trying to build a custom editor based on the "cloning medium with parchment" tutorial. I've built some blots and now I need to replace a blot with another blot.

Now your advice is to let the blockembed replace itself with another blot. Now I have two questions:

  • Which function inside the blockembed class should be overriden to do the replacement and how can I replace it?
  • When the upload is finished an event is triggered. At that moment I don't have a reference to the blot I need to replace, so how do I get a reference to the right blot, depending on the sequence number?

from delta.

jhchen avatar jhchen commented on August 23, 2024

Are you using just Parchment or Quill as well?

from delta.

 avatar commented on August 23, 2024

I am also using Quill. It would be great to see some examples, because for now I only see insertText and insertEmbed functions. I think I have to use Parchment.find to find the corresponding blot by a node. But if I have the blot, how do I delete it properly?

from delta.

 avatar commented on August 23, 2024

I think I've found a solution, correct me if I am wrong:

let node = document.querySelectorAll(`[data-sequence="${sequence}"]`);
let blot = Parchment.find(node);
let image = {src, alt, class, type, sequence};
blot.replaceWith('image', image);

Thanks for your feedback and wonderful editor!

from delta.

 avatar commented on August 23, 2024

Well after some testing, if I do

let node = document.querySelectorAll(`[data-sequence="${sequence}"]`);
let blot = Parchment.find(node);
let image = {src, alt, class, type, sequence};
blot.replaceWith('image', image);
console.log(quill.getContents());

after the replace, the content in the editor is updated, but the delta isn't.

from delta.

quill-bot avatar quill-bot commented on August 23, 2024

Closing inactive issue.

from delta.

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.