{"id":29178,"date":"2021-06-09T17:40:00","date_gmt":"2021-06-09T14:40:00","guid":{"rendered":"https:\/\/themewp.inform.click\/?p=29178"},"modified":"2021-10-17T16:23:56","modified_gmt":"2021-10-17T13:23:56","slug":"como-instalar-y-usar-trix-editor-en-laravel","status":"publish","type":"post","link":"https:\/\/themewp.inform.click\/es\/como-instalar-y-usar-trix-editor-en-laravel\/","title":{"rendered":"C\u00f3mo instalar y usar Trix Editor en Laravel"},"content":{"rendered":"<p>Al administrar un sitio web, siempre necesitamos un editor de texto enriquecido (WYSIWYG). Si desea agregar contenido descriptivo que contenga HTML, el editor WYSIWYG es la \u00fanica opci\u00f3n. No puede agregar ese tipo de contenido en el \u00e1rea de texto. Hay varios editores de texto enriquecido como <a href=\"https:\/\/themewp.inform.click\/es\/como-instalar-y-usar-tinymce-editor-en-laravel\/\" title=\"TinyMCE\" >TinyMCE<\/a>, <a href=\"https:\/\/themewp.inform.click\/es\/como-instalar-y-usar-ckeditor-en-laravel\/\" title=\"CKEditor\" >CKEditor<\/a> disponibles en Internet. <a href=\"https:\/\/trix-editor.org\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Trix Editor<\/a> es uno de ellos. Como desarrollador, debes haber o\u00eddo hablar de Basecamp. El editor Trix se usa en Basecamp para administrar contenido. Esto es suficiente para decir sobre la popularidad del editor Trix.<\/p>\n<p>En este art\u00edculo, estudiamos c\u00f3mo instalar y usar el editor Trix en Laravel. Tambi\u00e9n veremos c\u00f3mo subir im\u00e1genes en el editor de Trix.<\/p>\n<h3>Instalaci\u00f3n de Trix Editor<\/h3>\n<p>Para comenzar con Trix, primero debe incluir su archivo JS y CSS. Puede encontrar estos archivos en su <a href=\"https:\/\/github.com\/basecamp\/trix\/tree\/master\/dist\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">p\u00e1gina de GitHub en el directorio dist<\/a>. Descarga 2 archivos <code>trix.js<\/code>y <code>trix.css<\/code>los requerir\u00e1 pronto.<\/p>\n<p>Como necesitamos incluir activos, cree una carpeta &#8216;js&#8217; y &#8216;css&#8217; dentro del directorio p\u00fablico de su proyecto Laravel. Coloque los archivos JS y CSS copiados en los directorios respectivos.<\/p>\n<p>La carga de im\u00e1genes en el servidor usando Trix requiere que agregue c\u00f3digo JavaScript. As\u00ed que cree un <code>attachments.js<\/code>archivo y gu\u00e1rdelo en la <code>public\/js<\/code>carpeta. Agregaremos c\u00f3digo a este archivo en la parte posterior del tutorial.<\/p>\n<p>Agregue el siguiente HTML en el archivo blade que mostrar\u00e1 el editor Trix.<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n\u00a0\u00a0\u00a0\u00a0&lt;meta name=\"csrf-token\" content=\"{{ csrf_token() }}\"&gt;\n\u00a0\u00a0\u00a0\u00a0&lt;link rel=\"stylesheet\" href=\"{{ asset('\/css\/trix.css') }}\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\u00a0\u00a0\u00a0\u00a0&lt;form method=\"post\" action={{ url('ROUTE_HERE') }}&gt;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{{ csrf_field() }}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;p&gt;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;input id=\"x\" type=\"hidden\" name=\"content\" value=\"\" \/&gt;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;trix-editor input=\"x\"&gt;&lt;\/trix-editor&gt;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/p&gt;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;input type=\"submit\" name=\"submit\" value=\"Submit\" \/&gt;\n\u00a0\u00a0\u00a0\u00a0&lt;\/form&gt;\n\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0&lt;script src=\"{{ asset('js\/trix.js') }}\"&gt;&lt;\/script&gt;\n\u00a0\u00a0\u00a0\u00a0&lt;script src=\"{{ asset('js\/attachments.js') }}\"&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>El HTML anterior mostrar\u00e1 las Trix en su p\u00e1gina web como se muestra en la captura de pantalla a continuaci\u00f3n.<\/p>\n<p>Observe, colocamos el editor Trix en HTML usando la <code>trix-editor<\/code>etiqueta. El campo oculto con el nombre &#8216;contenido&#8217; se usa para obtener o mostrar contenido en Trix Editor. Cuando escribimos algo dentro de Trix, el mismo contenido se establece en este campo oculto. En el lado del servidor, obtendremos el contenido del editor de Trix usando la siguiente declaraci\u00f3n.<\/p>\n<pre><code>echo $request-&gt;input('content');<\/code><\/pre>\n<p>De la misma manera, si desea mostrar contenido precargado en Trix, establezca el valor en el campo oculto de la siguiente manera.<\/p>\n<pre><code>&lt;input id=\"x\" type=\"hidden\" name=\"content\" value=\"&lt;h1&gt;This is content&lt;\/h1&gt;\" \/&gt;<\/code><\/pre>\n<h3>Cargar imagen en Trix Editor<\/h3>\n<p>Si desea utilizar im\u00e1genes en el contenido, por supuesto, debe cargar la imagen en el servidor. El editor Trix proporciona un evento <code>trix-attachment-add<\/code>mediante el cual cargaremos la imagen a trav\u00e9s de Ajax. Definamos una ruta para ello.<\/p>\n<pre><code>Route::post('upload', 'ProductController@upload');<\/code><\/pre>\n<p>Ajuste el nombre del controlador en la ruta. En el m\u00e9todo de carga de Controller, escriba el c\u00f3digo de la siguiente manera.<\/p>\n<pre><code>public function upload(Request $request)\n{\n\u00a0\u00a0\u00a0\u00a0if($request-&gt;hasFile('file')) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/get filename with extension\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$filenamewithextension = $request-&gt;file('file')-&gt;getClientOriginalName();\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/get filename without extension\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/get file extension\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$extension = $request-&gt;file('file')-&gt;getClientOriginalExtension();\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/filename to store\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$filenametostore = $filename.'_'.time().'.'.$extension;\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/Upload File\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$request-&gt;file('file')-&gt;storeAs('public\/uploads', $filenametostore);\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ you can save image path below in database\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$path = asset('storage\/uploads\/'.$filenametostore);\n\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo $path;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0exit;\n\u00a0\u00a0\u00a0\u00a0}\n}<\/code><\/pre>\n<p>Aqu\u00ed, estoy almacenando im\u00e1genes en la <code>public\/uploads<\/code>carpeta. Es posible que deba crear un enlace simb\u00f3lico a la carpeta de almacenamiento con el comando:<\/p>\n<pre><code>php artisan storage:link<\/code><\/pre>\n<p>Hemos creado un <code>attachments.js<\/code>archivo que se utilizar\u00e1 para realizar una llamada Ajax en el evento <code>trix-attachment-add<\/code>. Agregue el siguiente c\u00f3digo en el <code>attachments.js<\/code>archivo.<\/p>\n<pre><code>(function() {\n\u00a0\u00a0\u00a0\u00a0var HOST = \"http:\/\/localhost:8000\/upload\"; \/\/pass the route\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0addEventListener(\"trix-attachment-add\", function(event) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (event.attachment.file) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uploadFileAttachment(event.attachment)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0})\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0function uploadFileAttachment(attachment) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uploadFile(attachment.file, setProgress, setAttributes)\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0function setProgress(progress) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0attachment.setUploadProgress(progress)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0function setAttributes(attributes) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0attachment.setAttributes(attributes)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0function uploadFile(file, progressCallback, successCallback) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var formData = createFormData(file);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var xhr = new XMLHttpRequest();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0xhr.open(\"POST\", HOST, true);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0xhr.setRequestHeader( 'X-CSRF-TOKEN', getMeta( 'csrf-token') );\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0xhr.upload.addEventListener(\"progress\", function(event) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var progress = event.loaded \/ event.total * 100\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0progressCallback(progress)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0})\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0xhr.addEventListener(\"load\", function(event) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var attributes = {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0url: xhr.responseText,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0href: xhr.responseText + \"?content-disposition=attachment\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0successCallback(attributes)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0})\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0xhr.send(formData)\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0function createFormData(file) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var data = new FormData()\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data.append(\"Content-Type\", file.type)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data.append(\"file\", file)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return data\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\n\u00a0\u00a0\u00a0\u00a0function getMeta(metaName) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0const metas = document.getElementsByTagName('meta');\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (let i = 0; i &lt; metas.length; i++) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (metas[i].getAttribute('name') === metaName) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return metas[i].getAttribute('content');\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return '';\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n})();<\/code><\/pre>\n<p>En el c\u00f3digo anterior, debe pasar la ruta a la <code>HOST<\/code>variable. Laravel requiere un token CSRF en cada solicitud. Obtenemos este token CSRF de la metaetiqueta HTML que agregamos en el archivo blade. Cuando agrega la imagen en Trix, realiza una llamada Ajax, carga la imagen en el servidor y devuelve la ruta de la imagen cargada.<\/p>\n<p>Espero que entiendas c\u00f3mo instalar y usar el editor Trix en Laravel. Comparta sus pensamientos y sugerencias en la secci\u00f3n de comentarios a continuaci\u00f3n.<\/p>\n<h4>Art\u00edculos relacionados<\/h4>\n<ul>\n<li><a href=\"https:\/\/themewp.inform.click\/es\/como-instalar-y-usar-ckeditor-en-laravel\/\" title=\"C\u00f3mo instalar y usar CKEditor en Laravel\">C\u00f3mo instalar y usar CKEditor en Laravel<\/a><\/li>\n<li><a href=\"https:\/\/themewp.inform.click\/es\/como-instalar-y-usar-tinymce-editor-en-laravel\/\" title=\"C\u00f3mo instalar y usar TinyMCE Editor en Laravel\">C\u00f3mo instalar y usar TinyMCE Editor en Laravel<\/a><\/li>\n<li><a href=\"https:\/\/themewp.inform.click\/es\/cambiar-el-tamano-de-la-imagen-en-laravel-usando-la-biblioteca-de-imagenes-de-intervencion\/\" title=\"Cambiar el tama\u00f1o de la imagen en Laravel usando la biblioteca de im\u00e1genes de intervenci\u00f3n\">Cambiar el tama\u00f1o de la imagen en Laravel usando la biblioteca de im\u00e1genes de intervenci\u00f3n<\/a><\/li>\n<\/ul>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">Fuente de grabaci\u00f3n:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/artisansweb.net\" class=\"external external_icon\">artisansweb.net<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00bfQuieres integrar el editor Trix en Laravel? En este art\u00edculo, estudiamos c\u00f3mo instalar y usar el editor Trix en Laravel. Tambi\u00e9n vemos imagen<\/p>\n","protected":false},"author":1,"featured_media":20291,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[495],"tags":[849],"class_list":["post-29178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-affiai-es"],"_links":{"self":[{"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/posts\/29178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/comments?post=29178"}],"version-history":[{"count":0,"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/posts\/29178\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/media\/20291"}],"wp:attachment":[{"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/media?parent=29178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/categories?post=29178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themewp.inform.click\/es\/wp-json\/wp\/v2\/tags?post=29178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}