Search code examples
phplaraveltinymcevoyager

Can't upload photos via the (TinyMCE) by using laravel voyager page block plugin


why i can not insert photos from my computer for page block though (TinyMCE). Those, when adding a photo, nothing happens; it works only if I insert a link with a photoenter image description here enter image description here


Solution

  • This issue create when we are using page block plugin in laravel voyager.i try & this solution is work form me

    In config/voyager.php add an additional JS file 'additional_js' => [ 'js/tinymce.js', ],

    Then create your tinymce.js file with the following code (adjust to your needs of course):

    function tinymce_init_callback(editor)
    {
      editor.remove();
      editor = null;
    
      tinymce.init({
        menubar: false,
        selector:'textarea.richTextBox',
        skin_url: $('meta[name="assets-path"]').attr('content')+'?path=js/skins/voyager',
        min_height: 450,
        resize: 'vertical',
        plugins: 'link, image, code, table, textcolor, lists',
        extended_valid_elements : 'input[id|name|value|type|class|style|required|placeholder|autocomplete|onclick]',
        file_picker_callback: function (cb, value, meta) {
          var input = document.createElement('input');
          input.setAttribute('type', 'file');
          input.setAttribute('accept', 'image/*');
    
          /*
            Note: In modern browsers input[type="file"] is functional without
            even adding it to the DOM, but that might not be the case in some older
            or quirky browsers like IE, so you might want to add it to the DOM
            just in case, and visually hide it. And do not forget do remove it
            once you do not need it anymore.
          */
    
          input.onchange = function () {
            var file = this.files[0];
    
            var reader = new FileReader();
            reader.onload = function () {
              /*
                Note: Now we need to register the blob in TinyMCEs image blob
                registry. In the next release this part hopefully won't be
                necessary, as we are looking to handle it internally.
              */
              var id = 'blobid' + (new Date()).getTime();
              var blobCache =  tinymce.activeEditor.editorUpload.blobCache;
              var base64 = reader.result.split(',')[1];
              var blobInfo = blobCache.create(id, file, base64);
              blobCache.add(blobInfo);
    
              /* call the callback and populate the Title field with the file name */
              cb(blobInfo.blobUri(), { title: file.name });
            };
            reader.readAsDataURL(file);
          };
    
          input.click();
        },
        toolbar: 'styleselect bold italic underline | forecolor backcolor | alignleft aligncenter alignright | bullist numlist outdent indent | link image table | code',
        convert_urls: false,
        image_caption: true,
        image_title: true,
      });
    }