laravel-trix icon indicating copy to clipboard operation
laravel-trix copied to clipboard

How to load content from db to trix, to edit existing content?

Open filipdak opened this issue 4 years ago • 9 comments

Hello, Everything works fine, but cant figure out one think - how to load content to trix editor? I made in app option to create and edit article content. For example here I geting passed to view title: <input type="text" id='title' name='title' value="{{$article->title}}"></br>

But how load title to trix, to have chance to edit it using wysiwyg? @trix(\App\Article::class, 'title')

filipdak avatar Jan 06 '20 09:01 filipdak

@filipdak check Render Existing Model Docs

amaelftah avatar Jan 06 '20 10:01 amaelftah

Thanks for quick anserw, but still one thing - render methods loads content from db table trix_ritch_texts? In my case render methods show only wysiwyg editor but no content included - i think the problem is that my articles are stored only in articles table and trix_ritch_texts is still empty.

filipdak avatar Jan 06 '20 10:01 filipdak

@filipdak i think this is the reason . maybe i will try to implement a feature to solve this problem but for current time i have two thoughts on that .

  • first you can move the content of articles table to trix_rich_texts table .

  • second you can abandon trix package for now till i figure something that only renders text if it's not from DB

amaelftah avatar Jan 06 '20 10:01 amaelftah

This work for me!, in my blade file Show Video: https://www.loom.com/share/7ce5f1a4c7b34d5f878a9e2fb3825df4

    <script type="text/javascript">

        function setValue(text = '') {
            const trixEditor = document.querySelector("trix-editor")
            trixEditor.editor.insertHTML(text);
        }

        document.addEventListener("DOMContentLoaded", function (event) {
            setValue('HERE MY DATA RAW HTML')
        });


    </script>

leifermendez avatar Feb 26 '20 14:02 leifermendez

@leifermendez thanks for sharing

amaelftah avatar Feb 27 '20 02:02 amaelftah

@leifermendez your approach won't work if you use any sort of HTML string as setValue argument. In order to insert a string containing special chars into Trix text box (from Laravel model), you must do some weird stuff like that:

setValue( atob( "{{ base64_encode($contents) }}" ) );

mrneatly avatar Mar 26 '20 21:03 mrneatly

{!! Form::hidden('content', null, ['class' => 'form-control', 'id' => 'emailTemplate']) !!}
<trix-editor input="emailTemplate"></trix-editor>

use like the above. Trix method is not working properly when editing the existing data @trix(\App\Article::class, 'title')

harshuchilzy avatar Mar 31 '20 08:03 harshuchilzy

@manshu feel free to PR edit method or fork your own repo

amaelftah avatar Jun 07 '20 04:06 amaelftah

@manshu I created the package to solve my use case at first place then shared it with other developers. If it's half backed then either to PR the missing points or fork your own . But don't expect from open source maintainers to do your work when you don't have time

amaelftah avatar Jun 07 '20 04:06 amaelftah