Giter Site home page Giter Site logo

Comments (6)

kyuwoo-choi avatar kyuwoo-choi commented on July 19, 2024 9

you're doing it right.

The editor will give you two params blob & callback to addImageBlobHook.
I guess you already have done uploading blob.

next you can pass uploaded image url to callback like below

...
addImageBlobHook: function(blob, callback) {
  var uploadedImageURL = yourUploadingLogicHere(blob);
  callback(uploadedImageURL, 'alt text');
}
...

this will replace base64 encode image markdown like this.

[alt text](uploadedImageURLYouHaveFromLogic)

from tui.editor.

artemmolotov avatar artemmolotov commented on July 19, 2024 9

More complete example:

...
    hooks: {
        addImageBlobHook: this.onAddImageBlob(),
    },
...

uploadImage(blob) {
    let formData = new FormData();

    // file in a 'multipart/form-data' request
    formData.append(0, blob, blob.name);

    return fetch('/upload/image', {
        method: 'POST',
        body: formData
    }).then(response => {
        if (response.ok) {
            return response.json();
        }

        throw new Error('Server or network error');
    });
};

onAddImageBlob(blob, callback) {
    uploadImage(blob)
        .then(response => {
            if (!response.success) {
                throw new Error('Validation error');
            }

            callback(response.data.url, 'alt text');
        }).catch(error => {
            console.log(error);
        });
};

from tui.editor.

artemmolotov avatar artemmolotov commented on July 19, 2024 1

do you have the full code please ?

Hi, @2klm! I published all the code above, but you can try this one too, if the previous code doesn't work for you.

let editorElement = window.document.querySelector('#editor__container');

let editor = new TuiEditor({
    el: editorElement,
    hooks: {
        addImageBlobHook: (blob, callback) => {
            let formData = new FormData();

            // file in a 'multipart/form-data' request
            formData.append(0, blob, blob.name);

            fetch('/upload/image', {
                method: 'POST',
                body: formData
            }).then(response => {
                if (response.ok) {
                    return response.json();
                }

                throw new Error('Server or network error');
            }).then(response => {
                if (!response.success) {
                    throw new Error('Validation error');
                }

                callback(response.data.url, 'alt text');
            }).catch(error => {
                console.log(error);
            });
        },
    },
});

Be careful, please. This code requires your custom back-end implementation of the images storing and publishing. Example of the server response: {"success":true, "data":{"url":"https://some.domain/img1.png"}}.

from tui.editor.

artemmolotov avatar artemmolotov commented on July 19, 2024 1

Massive thanks @artemmolotov !!

Just a question, is fetch(/upload/image) the link to my PHP script or the destination folder ?

It's link to a your web route or script.

from tui.editor.

kyuwoo-choi avatar kyuwoo-choi commented on July 19, 2024

I'm closing this. If you have further question, feel free to reopen this.

from tui.editor.

ct1735x avatar ct1735x commented on July 19, 2024

More complete example:

...
    hooks: {
        addImageBlobHook: this.onAddImageBlob(),
    },
...

uploadImage(blob) {
    let formData = new FormData();

    // file in a 'multipart/form-data' request
    formData.append(0, blob, blob.name);

    return fetch('/upload/image', {
        method: 'POST',
        body: formData
    }).then(response => {
        if (response.ok) {
            return response.json();
        }

        throw new Error('Server or network error');
    });
};

onAddImageBlob(blob, callback) {
    uploadImage(blob)
        .then(response => {
            if (!response.success) {
                throw new Error('Validation error');
            }

            callback(response.data.url, 'alt text');
        }).catch(error => {
            console.log(error);
        });
};

do you have the full code please ?

from tui.editor.

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.