trix icon indicating copy to clipboard operation
trix copied to clipboard

Why image tag is cut off in trix editor component of livewire app?

Open sergeynilov opened this issue 11 months ago • 3 comments

Reading this https://devdojo.com/tnylea/laravel-livewire-trix-editor-component article I added trix-editor on laravel 10 / livewire 3 app / kubuntu 22.04 / Google Chrome Version 123 / trix 1.3.1 / . It works but until image is selected into trix editor - saving content all html from the point when image is selected is cut.

I have a trix editor component :

<div wire:ignore>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/trix/1.3.1/trix.min.css" />

    <input id="{{ $trixId }}" type="hidden" name="content" value="{{ $value }}">
    <trix-editor wire:ignore input="{{ $trixId }}"></trix-editor>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/trix/1.3.1/trix.min.js"></script>
</div>

<script>
    var trixEditor = document.getElementById("{{ $trixId }}")
    var mimeTypes = ["image/png", "image/jpeg", "image/jpg"];

    console.log('trixEditor::')
    console.log(trixEditor)


    addEventListener("trix-blur", function(event) {
        console.log('trix-blur  trixEditor.getAttribute(value)::')
        console.log(trixEditor.getAttribute('value'))

        @this.set('value', trixEditor.getAttribute('value'));
    })


    addEventListener("trix-file-accept", function(event) {
        if (! mimeTypes.includes(event.file.type) ) {
            // file type not allowed, prevent default upload
            return event.preventDefault();
        }
    });

    addEventListener("trix-attachment-add", function(event){
        uploadTrixImage(event.attachment);
    });

    function uploadTrixImage(attachment){
        // upload with livewire
    @this.upload(
        'photos',
        attachment.file,
        function (uploadedURL) {

            // We need to create a custom event.
            // This event will create a pause in thread execution until we get the Response URL from the Trix Component @completeUpload
            const trixUploadCompletedEvent = `trix-upload-completed:${btoa(uploadedURL)}`;
            const trixUploadCompletedListener = function(event) {
                attachment.setAttributes(event.detail);
                window.removeEventListener(trixUploadCompletedEvent, trixUploadCompletedListener);
            }

            window.addEventListener(trixUploadCompletedEvent, trixUploadCompletedListener);

            // call the Trix Component @completeUpload below
        @this.call('completeUpload', uploadedURL, trixUploadCompletedEvent);
        },
        function() {},
        function(event){
            attachment.setUploadProgress(event.detail.progress);
        },
    )
        // complete the upload and get the actual file URL
    }


</script>

and using of this component on blade form :

<div class="editor_field_block_wrapper">
    <div class="editor_field_block_device_splitter">
        <div class="w-4/12 pb-0 pl-2 md:pt-3 ">
            <label for="content" class="editor_field_block_device_label">
                Content <span class="editor_form_aria_required" aria-required="true"> * </span>
                <br>$content::{{ $content }}
            </label>
        </div>
        <div class="p-2 w-full" wire:ignore>
            @livewire('trix-editor', ['value' => $content, 'imagesPath' => $imagesPath])
            @error('content') <span class="editor_form_validation_error">{{ $message }}</span> @enderror
        </div>
    </div>
</div>

But even outputting $content var on the content I see :

enter image description here

Any hints why image is cleared and can I fix it ?

Checking output of trixEditor.getAttribute('value') var - I see image is not inside of trixEditor.getAttribute('value') : this html block is cut off :

  addEventListener("trix-blur", function(event) {
        console.log('trix-blur  trixEditor.getAttribute(value)::')
        console.log(trixEditor.getAttribute('value'))

        @this.set('value', trixEditor.getAttribute('value'));
    })

even in case if after adding image I add some text after image - I see this new text block

I suppose this is problem on trix side, not livewire ? Can it be some security issue ?

sergeynilov avatar Mar 24 '24 15:03 sergeynilov

This seems to be a duplicate of #1137?

michaellenaghan avatar Mar 27 '24 23:03 michaellenaghan

This seems to be a duplicate of #1137?

Yes, sorry. I tried to delete message on Feb 23, but did not find such option .

sergeynilov avatar Mar 28 '24 04:03 sergeynilov

Maybe close one of them then? (Just to be clear: I'm not part of the project, I was just taking a pass through the open issues to get a feel for its current state.)

michaellenaghan avatar Mar 28 '24 14:03 michaellenaghan