Editor.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. <template>
  2. <v-container>
  3. <div v-if="editor" class="menu-editor">
  4. <v-btn small @click="openImgModal()" plain>
  5. <v-icon>mdi-image </v-icon>
  6. </v-btn>
  7. <v-btn
  8. plain
  9. small
  10. @click="editor.chain().focus().toggleBold().run()"
  11. :class="{ 'is-active': editor.isActive('bold') }"
  12. >
  13. <v-icon> mdi-format-bold </v-icon>
  14. </v-btn>
  15. <v-btn
  16. plain
  17. small
  18. @click="editor.chain().focus().toggleItalic().run()"
  19. :class="{ 'is-active': editor.isActive('italic') }"
  20. >
  21. <v-icon> mdi-format-italic </v-icon>
  22. </v-btn>
  23. <v-btn
  24. plain
  25. small
  26. @click="editor.chain().focus().setParagraph().run()"
  27. :class="{ 'is-active': editor.isActive('paragraph') }"
  28. >
  29. <v-icon> mdi-format-paragraph </v-icon>
  30. </v-btn>
  31. <v-btn small plain @click="addInstagram">
  32. <v-icon>mdi-instagram </v-icon>
  33. </v-btn>
  34. <v-btn small plain @click="addIframe">
  35. <v-icon></v-icon>
  36. iframe
  37. </v-btn>
  38. <v-btn small plain @click="openGalleryModalInsert">
  39. <v-icon></v-icon>
  40. Галерея
  41. </v-btn>
  42. <v-btn
  43. small
  44. plain
  45. @click="openLinkModal"
  46. :class="{ 'is-active': editor.isActive('link') }"
  47. >
  48. ссылка
  49. </v-btn>
  50. <v-btn
  51. small
  52. plain
  53. @click="setTooltip"
  54. :class="{ 'is-active': editor.isActive('tooltip') }"
  55. >
  56. подсказка
  57. </v-btn>
  58. <v-btn
  59. plain
  60. small
  61. @click="editor.chain().focus().toggleHeading({ level: 1 }).run()"
  62. :class="{ 'is-active': editor.isActive('heading', { level: 1 }) }"
  63. >
  64. h1
  65. </v-btn>
  66. <v-btn
  67. small
  68. plain
  69. @click="editor.chain().focus().toggleHeading({ level: 2 }).run()"
  70. :class="{ 'is-active': editor.isActive('heading', { level: 2 }) }"
  71. >
  72. h2
  73. </v-btn>
  74. <v-btn
  75. small
  76. plain
  77. @click="editor.chain().focus().toggleHeading({ level: 3 }).run()"
  78. :class="{ 'is-active': editor.isActive('heading', { level: 3 }) }"
  79. >
  80. h3
  81. </v-btn>
  82. <v-btn
  83. plain
  84. small
  85. @click="editor.chain().focus().toggleHeading({ level: 4 }).run()"
  86. :class="{ 'is-active': editor.isActive('heading', { level: 4 }) }"
  87. >
  88. h4
  89. </v-btn>
  90. <v-btn
  91. plain
  92. small
  93. @click="editor.chain().focus().toggleHeading({ level: 5 }).run()"
  94. :class="{ 'is-active': editor.isActive('heading', { level: 5 }) }"
  95. >
  96. ИФ
  97. </v-btn>
  98. <v-btn
  99. plain
  100. small
  101. @click="editor.chain().focus().toggleHeading({ level: 6 }).run()"
  102. :class="{ 'is-active': editor.isActive('heading', { level: 6 }) }"
  103. >
  104. Выделить
  105. </v-btn>
  106. <v-btn
  107. plain
  108. small
  109. @click="editor.chain().focus().toggleBulletList().run()"
  110. :class="{ 'is-active': editor.isActive('bulletList') }"
  111. >
  112. <v-icon>mdi-format-list-bulleted</v-icon>
  113. </v-btn>
  114. <v-btn
  115. plain
  116. small
  117. @click="editor.chain().focus().toggleOrderedList().run()"
  118. :class="{ 'is-active': editor.isActive('orderedList') }"
  119. >
  120. <v-icon>mdi-format-list-numbered</v-icon>
  121. </v-btn>
  122. <v-btn
  123. plain
  124. small
  125. @click="editor.chain().focus().toggleBlockquote().run()"
  126. :class="{ 'is-active': editor.isActive('blockquote') }"
  127. >
  128. <v-icon>mdi-format-quote-close-outline</v-icon>
  129. </v-btn>
  130. <v-btn plain small @click="editor.chain().focus().clearNodes().unsetAllMarks().run()">
  131. <v-icon>mdi-format-clear</v-icon>
  132. </v-btn>
  133. <v-btn plain small @click="openHtmlModal">
  134. <v-icon>mdi-file-code-outline</v-icon>
  135. </v-btn>
  136. <v-btn plain small @click="editor.chain().focus().undo().run()">
  137. <v-icon>mdi-undo-variant</v-icon>
  138. </v-btn>
  139. <v-btn plain small @click="editor.chain().focus().redo().run()">
  140. <v-icon> mdi-redo-variant </v-icon>
  141. </v-btn>
  142. </div>
  143. <div class="editor" ref="editor">
  144. <editor-content @click.native="onClickEditor" :editor="editor" />
  145. </div>
  146. <modal name="link-modal" height="auto" v-if="editor">
  147. <v-text-field v-model="link.href" label="Ссылка"></v-text-field>
  148. <v-checkbox class="mt-0" v-model="link.nofollow" label="No follow"></v-checkbox>
  149. <v-btn :disabled="!link.href" @click="setLink" text color="primary">Ок</v-btn>
  150. <v-btn @click="unsetLink" text color="red" v-if="editor.isActive('link')">Удалить</v-btn>
  151. </modal>
  152. <modal name="img-modal" @closed="image.messages = []">
  153. <v-file-input
  154. label="Картинка:"
  155. type="file"
  156. accept="image/*"
  157. ref="inputEditor"
  158. :error-messages="image.messages"
  159. v-model="image.file"
  160. ></v-file-input>
  161. <v-btn @click="addImage">Добавить изображение</v-btn>
  162. </modal>
  163. <modal
  164. height="auto"
  165. name="gallery-modal"
  166. @closed="gallery.messages = []"
  167. :edit-mode="false"
  168. >
  169. <div class="gallery-container" ref="galleryContainer">
  170. <draggable v-model="gallery.list">
  171. <transition-group type="transition" name="flip-list">
  172. <div
  173. class="gallery-item"
  174. v-for="(img, index) in gallery.list"
  175. :key="img.url"
  176. >
  177. <img :src="img.url" :alt="img.name" />
  178. <v-text-field
  179. prepend-icon="mdi-image-outline"
  180. placeholder="Название изображения"
  181. v-model="img.name"
  182. ></v-text-field>
  183. <div
  184. class="gallery-item_delete"
  185. @click="gallery.list.splice(index, 1)"
  186. >
  187. <v-icon> mdi-delete </v-icon>
  188. </div>
  189. </div>
  190. </transition-group>
  191. </draggable>
  192. </div>
  193. <v-file-input
  194. @change="addImageToGallery"
  195. small-chips
  196. multiple
  197. label="Картинка:"
  198. type="file"
  199. accept="image/*"
  200. ref="galleryFileInput"
  201. :error-messages="gallery.messages"
  202. v-model="gallery.files"
  203. ></v-file-input>
  204. <v-btn
  205. text
  206. color="primary"
  207. @click="insertGallery"
  208. v-if="!gallery.editMode"
  209. :disabled="!gallery.list.length"
  210. >
  211. Вставить галерею в редактор
  212. </v-btn>
  213. <v-btn
  214. text
  215. color="primary"
  216. @click="editGallery"
  217. v-else
  218. :disabled="!gallery.list.length"
  219. >
  220. Сохранить галерею
  221. </v-btn>
  222. </modal>
  223. <modal name="html-modal" width="80%" height="auto">
  224. <v-textarea v-model="content" rows="10" outlined label="Исходный код HTML"></v-textarea>
  225. <v-btn @click="saveHtml" text color="primary" class="mt-0">Сохранить</v-btn>
  226. </modal>
  227. </v-container>
  228. </template>
  229. <script>
  230. import Iframe from "./iframe.ts";
  231. import Tooltip from "./tooltip.ts";
  232. import Link from "./link.ts";
  233. import Gallery from "./gallery";
  234. import Focus from "@tiptap/extension-focus";
  235. import Image from "@tiptap/extension-image";
  236. import { Editor, EditorContent } from "@tiptap/vue-2";
  237. import StarterKit from "@tiptap/starter-kit";
  238. import Typography from "@tiptap/extension-typography";
  239. import draggable from "vuedraggable";
  240. export default {
  241. components: {
  242. EditorContent,
  243. draggable,
  244. },
  245. data() {
  246. return {
  247. editor: null,
  248. content: this.value,
  249. link: {
  250. href: '',
  251. nofollow: false,
  252. },
  253. image: {
  254. file: null,
  255. messages: [],
  256. },
  257. gallery: {
  258. files: null,
  259. messages: [],
  260. list: [],
  261. editMode: false,
  262. editID: null,
  263. },
  264. galleries: [],
  265. };
  266. },
  267. props: {
  268. value: {
  269. type: String,
  270. default: "",
  271. },
  272. },
  273. mounted() {
  274. this.editor = new Editor({
  275. content: this.value,
  276. extensions: [
  277. Gallery,
  278. Iframe,
  279. Tooltip,
  280. StarterKit,
  281. Typography,
  282. Link,
  283. Focus.configure({
  284. className: "has-focus",
  285. mode: "all",
  286. }),
  287. Image.configure({
  288. inline: true,
  289. }),
  290. ,
  291. ],
  292. onUpdate: ({ editor }) => {
  293. this.$emit("input", editor.getHTML());
  294. },
  295. onCreate: () => {
  296. const galleriesDOM = this.$refs.editor.querySelectorAll(".editor-gallery");
  297. galleriesDOM.forEach((galleryDOM) => {
  298. const imagesDOM = galleryDOM.querySelectorAll("img[src]");
  299. const images = [...imagesDOM].map((img) => ({
  300. url: img.src,
  301. name: img.alt,
  302. }));
  303. const gallery = { id: galleryDOM.id, list: images };
  304. this.galleries.push(gallery);
  305. });
  306. },
  307. });
  308. this.editor.commands.setContent(this.value);
  309. },
  310. beforeDestroy() {
  311. if (this.editor) {
  312. this.editor.destroy();
  313. }
  314. },
  315. methods: {
  316. openHtmlModal() {
  317. this.content = this.editor.getHTML();
  318. this.$modal.show('html-modal');
  319. },
  320. saveHtml() {
  321. this.editor.commands.setContent(this.content);
  322. this.$modal.hide('html-modal');
  323. },
  324. openLinkModal() {
  325. this.link.href = '';
  326. this.link.nofollow = false;
  327. if (this.editor.isActive("link")) {
  328. const attrs = this.editor.getAttributes('link');
  329. this.link.href = attrs.href;
  330. this.link.nofollow = attrs.rel.includes('nofollow');
  331. }
  332. this.$modal.show('link-modal');
  333. },
  334. setLink() {
  335. let rel = this.link.nofollow ? 'noopener noreferrer nofollow' : 'noopener noreferrer';
  336. this.editor
  337. .chain()
  338. .focus()
  339. .extendMarkRange("link")
  340. .unsetLink()
  341. .setLink({ href: this.link.href, rel })
  342. .run();
  343. this.$modal.hide('link-modal');
  344. },
  345. unsetLink() {
  346. this.editor.chain().focus().unsetLink().run();
  347. this.$modal.hide('link-modal');
  348. },
  349. setTooltip() {
  350. let dataTitle = '', dataText = '';
  351. if (this.editor.isActive("tooltip")) {
  352. const attrs = this.editor.getAttributes('tooltip');
  353. dataTitle = attrs['data-title'];
  354. dataText = attrs['data-text'];
  355. }
  356. const title = window.prompt("Введите название подсказки", dataTitle);
  357. if(!title) return
  358. const text = window.prompt("Введите текст подсказки", dataText);
  359. if(!text) return;
  360. this.editor
  361. .chain()
  362. .focus()
  363. .extendMarkRange("tooltip")
  364. .unsetTooltip()
  365. .setTooltip({ "data-title": title, "data-text": text })
  366. .run();
  367. },
  368. addIframe() {
  369. // Метод добавляет не только фреймы, но и любой html
  370. const url = window.prompt("URL");
  371. if (url) this.editor.commands.insertContent(url);
  372. },
  373. addInstagram() {
  374. const url = window.prompt("URL");
  375. if (url){
  376. if(url.slice(-1) == '/') {
  377. const newsUrl = url.substring(0, url.length - 1);
  378. const text = '<iframe src="' + newsUrl + '/embed" class="iframe-instagram" width="320" frameborder="0" scrolling="auto"></iframe>';
  379. this.editor.commands.insertContent(text);
  380. }else{
  381. const text = '<iframe src="' + url + '/embed" class="iframe-instagram" width="320" frameborder="0" scrolling="auto"></iframe>';
  382. this.editor.commands.insertContent(text);
  383. }
  384. }
  385. },
  386. onClickEditor(event) {
  387. if (!event.target.classList.contains("button")) return;
  388. const gallery = event.target.closest(".editor-gallery");
  389. const galleryID = gallery.id;
  390. if (event.target.classList.contains("button-edit")) {
  391. this.openGalleryModalEdit(galleryID);
  392. }
  393. if (event.target.classList.contains("button-delete")) {
  394. this.galleries = this.galleries.filter((g) => g.id != galleryID);
  395. gallery.remove();
  396. }
  397. },
  398. addImage(file) {
  399. if (!this.image.file) return;
  400. this.image.messages = [];
  401. this.uploadImage(this.image.file)
  402. .then((url) => {
  403. this.editor.chain().focus().setImage({ src: url }).run();
  404. this.$modal.hide("img-modal");
  405. })
  406. .catch((err) => {
  407. this.image.messages.push(err);
  408. })
  409. .finally(() => {
  410. this.image.file = null;
  411. });
  412. },
  413. addImageToGallery() {
  414. if (!this.gallery.files) return;
  415. this.gallery.messages = [];
  416. const promises = this.gallery.files.map((file) => this.uploadImage(file));
  417. Promise.all(promises)
  418. .then((urls) => {
  419. urls.forEach((url) => this.gallery.list.push({ url, name: "" }));
  420. })
  421. .catch((err) => {
  422. this.gallery.messages.push(err);
  423. })
  424. .finally(() => {
  425. this.$refs.galleryFileInput.blur();
  426. this.gallery.files = null;
  427. });
  428. },
  429. openImgModal() {
  430. this.$modal.show("img-modal");
  431. },
  432. openGalleryModalEdit(id) {
  433. if (!id) return;
  434. this.gallery.editMode = true;
  435. this.gallery.editID = id;
  436. this.gallery.list = this.galleries.find((g) => g.id == id).list.slice();
  437. this.$modal.show("gallery-modal");
  438. },
  439. openGalleryModalInsert() {
  440. this.gallery.editMode = false;
  441. this.gallery.editID = null;
  442. this.gallery.list = [];
  443. this.$modal.show("gallery-modal");
  444. },
  445. editGallery() {
  446. const id = this.gallery.editID;
  447. const galleryInEditor = this.$refs.editor.querySelector(`#${id} .content`);
  448. galleryInEditor.innerHTML = this.imagesHTML;
  449. this.galleries.find((g) => g.id == id).list = this.gallery.list;
  450. this.$modal.hide("gallery-modal");
  451. },
  452. insertGallery() {
  453. const id = `gallery-${this.galleries.length}`;
  454. const gallery = { id, list: this.gallery.list };
  455. this.galleries.push(gallery);
  456. this.$modal.hide("gallery-modal");
  457. const content = `<div class="gallery" id="${id}">${this.imagesHTML}</div>`;
  458. this.editor.commands.insertContent(content);
  459. },
  460. uploadImage(file) {
  461. return new Promise((resolve, reject) => {
  462. let image = new window.Image();
  463. image.src = window.URL.createObjectURL(file);
  464. image.onload = () => {
  465. let width = image.width;
  466. let height = image.height;
  467. if (width < 200 || height < 200) {
  468. reject("Размеры изображения должны быть не менее 200x200");
  469. }
  470. if(width > 1300){
  471. let ratioPhoto;
  472. if(width > height){
  473. ratioPhoto = 1300 / width
  474. }else{
  475. ratioPhoto = 900 / height
  476. }
  477. width = ratioPhoto * width;
  478. height = ratioPhoto * height;
  479. }
  480. let formData = new FormData();
  481. formData.append("file", file);
  482. formData.append("destination_width", width);
  483. formData.append("destination_height", height);
  484. formData.append("title", "example");
  485. this.$axios
  486. .$post("authorized/admin/files/upload/image/news/raw", formData, {
  487. headers: {
  488. "Content-Type": `multipart/form-data;`,
  489. },
  490. })
  491. .then((res) => {
  492. let url = "https://api.amic.ru/" + res.data.relative_path;
  493. resolve(url);
  494. })
  495. .catch((err) => {
  496. reject(err.errors.join("* "));
  497. });
  498. };
  499. });
  500. },
  501. },
  502. computed: {
  503. imagesHTML() {
  504. return this.gallery.list
  505. .map((img) => `<img src="${img.url}" alt="${img.name}"></img>`)
  506. .join("");
  507. },
  508. },
  509. };
  510. </script>
  511. <style lang="less">
  512. .flip-list-move {
  513. transition: transform 0.2s;
  514. }
  515. //Gallery in editor
  516. .editor-gallery {
  517. .content {
  518. display: flex;
  519. background: #f3f1f1;
  520. padding: 10px;
  521. flex-wrap: wrap;
  522. border-radius: 20px;
  523. pointer-events: none;
  524. img {
  525. width: 10%;
  526. height: auto;
  527. margin: 0 10px;
  528. object-fit: contain;
  529. }
  530. img.ProseMirror-separator {
  531. display: none !important;
  532. }
  533. }
  534. .button {
  535. margin: 5px;
  536. }
  537. }
  538. //Gallery in modal
  539. .gallery-container {
  540. display: flex;
  541. flex-wrap: wrap;
  542. align-items: center;
  543. max-height: 600px;
  544. overflow: auto;
  545. img {
  546. max-width: 25%;
  547. margin: 0 5px;
  548. }
  549. }
  550. .gallery-item {
  551. cursor: move;
  552. position: relative;
  553. &_delete {
  554. cursor: pointer;
  555. position: absolute;
  556. top: 0;
  557. right: 5px;
  558. }
  559. }
  560. //Tooltip in editor
  561. span.tooltip {
  562. background: #f3f1f1;
  563. border-bottom: 1px dashed black;
  564. html.dark-mode & {
  565. background: rgba(243, 241, 241, 0.2);
  566. border-bottom: 1px dashed white;
  567. }
  568. }
  569. .ProseMirror {
  570. min-height: 800px;
  571. max-height: 800px;
  572. overflow-y: scroll;
  573. outline: none;
  574. padding: 0px 15px;
  575. &::-webkit-scrollbar {
  576. width: 10px;
  577. &-track {
  578. background-color: rgba(0, 0, 0, 0.1);
  579. }
  580. &-thumb {
  581. background-color: black;
  582. border-radius: 15px;
  583. }
  584. }
  585. }
  586. .editor {
  587. margin: 10px auto 0;
  588. border: 1px solid rgba(0, 0, 0, 0.5);
  589. border-radius: 15px;
  590. min-height: 600px;
  591. overflow: hidden;
  592. p,
  593. h1,
  594. h2,
  595. h3,
  596. h4 {
  597. font: "Golos Text", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
  598. Helvetica, Ubuntu, Arial, sans-serif;
  599. }
  600. h1 {
  601. font: 36px/43px "Golos Text", -apple-system, BlinkMacSystemFont, "Segoe UI",
  602. Roboto, Helvetica, Ubuntu, Arial, sans-serif;
  603. font-weight: 700;
  604. }
  605. p {
  606. margin-bottom: 20px;
  607. font: 18px/29px "Golos Text", -apple-system, BlinkMacSystemFont, "Segoe UI",
  608. Roboto, Helvetica, Ubuntu, Arial, sans-serif;
  609. padding: 3px;
  610. }
  611. blockquote p {
  612. font-size: 28px;
  613. line-height: 140%;
  614. }
  615. }
  616. .menu-editor {
  617. width: 100%;
  618. display: flex;
  619. justify-content: center;
  620. flex-wrap: wrap;
  621. margin: 0 auto;
  622. margin-top: 40px;
  623. button {
  624. margin-left: 10px;
  625. margin-bottom: 15px;
  626. }
  627. }
  628. .has-focus {
  629. outline: 1px solid #bdb4b4;
  630. border-radius: 2px;
  631. }
  632. .v-btn:not(.v-btn--round).v-size--small {
  633. min-width: 15px;
  634. padding: 5px 5px;
  635. &:hover {
  636. background: black;
  637. color: white;
  638. }
  639. margin-bottom: 0;
  640. }
  641. .no-wrap {
  642. flex-wrap: nowrap !important;
  643. }
  644. .editor {
  645. img {
  646. max-width: 100% !important;
  647. }
  648. a {
  649. pointer-events: none;
  650. }
  651. }
  652. .vm--modal {
  653. padding: 15px;
  654. }
  655. </style>