Editor.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <script setup lang="ts">
  2. import "remixicon/fonts/remixicon.css";
  3. import "../style/index.css";
  4. import "../style/table.scss";
  5. import { computed, onBeforeUnmount, onMounted, provide, ref, shallowRef, watch } from "vue";
  6. import { useThemeVars } from "naive-ui";
  7. import { EditorContent, BubbleMenu, EditorOptions } from "@tiptap/vue-3";
  8. import { useDebounceFn, useThrottleFn } from "@vueuse/core";
  9. import { NMessageProvider, NDialogProvider } from "naive-ui";
  10. import VividMenu from "./components/VividMenu.vue";
  11. import VividBubbleMenu from "./components/VividBubbleMenu.vue";
  12. import VividFooter from "./components/VividFooter.vue";
  13. import { Editor } from "@tiptap/vue-3";
  14. import { CellSelection } from "prosemirror-tables";
  15. import { TextSelection } from "@tiptap/pm/state";
  16. import { Extension, Mark, Node, Editor as TiptapEditor } from "@tiptap/core";
  17. const vars = useThemeVars();
  18. const props = defineProps({
  19. page: {
  20. type: Boolean,
  21. default: false,
  22. },
  23. dark: {
  24. type: Boolean,
  25. default: false,
  26. },
  27. bubbleMenu: {
  28. type: Boolean,
  29. default: true,
  30. },
  31. modelValue: {
  32. type: String,
  33. default: "",
  34. },
  35. to: {
  36. type: String,
  37. required: false,
  38. default: "",
  39. },
  40. readonly: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. handleImageUpload: {
  45. type: Function,
  46. required: false,
  47. },
  48. handleVideoUpload: {
  49. type: Function,
  50. required: false,
  51. },
  52. handlePackageUpload: {
  53. type: Function,
  54. required: false,
  55. },
  56. });
  57. let internalExt: (Extension | Node | Mark)[] = [];
  58. const editor = shallowRef<Editor>();
  59. const words = ref(0);
  60. const characters = ref(0);
  61. const fullscreen = ref(false);
  62. const editorBox = ref<any>(null);
  63. const editorBoxParent = ref<any>(null);
  64. const isFocused = ref(false);
  65. const emit = defineEmits(["update:modelValue", "change"]);
  66. provide("useExtension", useExtension);
  67. provide("removeExtension", removeExtension);
  68. provide("editorInstance", editor);
  69. const updateEditorWordCount = useDebounceFn(() => {
  70. words.value = editor.value?.storage.characterCount.words() || 0;
  71. characters.value = editor.value?.storage.characterCount.characters() || 0;
  72. }, 300);
  73. function useExtension(ext: Extension | Node | Mark) {
  74. if (internalExt.filter((e) => e === ext).length) {
  75. return;
  76. }
  77. internalExt.push(ext);
  78. }
  79. function removeExtension(extName: string) {
  80. const index = internalExt.findIndex((e) => e.name === extName);
  81. if (index > -1) {
  82. internalExt.splice(index, 1);
  83. }
  84. }
  85. const onUpdate = useDebounceFn((editor: TiptapEditor) => {
  86. // HTML
  87. emit("update:modelValue", editor.getHTML());
  88. // JSON
  89. // this.$emit('update:modelValue', this.editor.getJSON())
  90. updateEditorWordCount();
  91. }, 300);
  92. function initEditor() {
  93. const opt: Partial<EditorOptions> = {
  94. content: props.modelValue,
  95. editable: !props.readonly,
  96. extensions: internalExt,
  97. onUpdate: ({ editor }) => {
  98. onUpdate(editor);
  99. },
  100. onFocus: () => {
  101. isFocused.value = true;
  102. },
  103. onBlur: () => {
  104. isFocused.value = false;
  105. },
  106. onSelectionUpdate: ({ editor }) => {
  107. },
  108. };
  109. editor.value = new Editor(opt);
  110. editor.value.storage.fullscreen = fullscreen;
  111. emit("update:modelValue", editor.value.getHTML());
  112. emit("change", editor.value.getHTML());
  113. updateEditorWordCount();
  114. }
  115. onMounted(() => {
  116. initEditor();
  117. });
  118. watch(
  119. () => props.readonly,
  120. (value) => {
  121. if (editor.value) {
  122. editor.value.setEditable(!value);
  123. }
  124. },
  125. );
  126. const onValueChange = useThrottleFn((value: string) => {
  127. if (!editor.value) {
  128. return;
  129. }
  130. // HTML
  131. const isSame = editor.value.getHTML() === value;
  132. // JSON
  133. // const isSame = JSON.stringify(this.editor.getJSON()) === JSON.stringify(value)
  134. if (isSame) {
  135. return;
  136. }
  137. editor.value.commands.setContent(value, false);
  138. }, 200);
  139. watch(
  140. () => props.modelValue,
  141. onValueChange,
  142. );
  143. watch(fullscreen, () => {
  144. if (fullscreen.value === true) {
  145. if (props.to) {
  146. editorBoxParent.value = editorBox.value.parentNode;
  147. const to = document.getElementById(props.to);
  148. if (to) {
  149. to.append(editorBox.value);
  150. }
  151. }
  152. } else {
  153. if (props.to) {
  154. editorBoxParent.value.append(editorBox.value);
  155. }
  156. }
  157. });
  158. onBeforeUnmount(() => {
  159. editor.value?.destroy();
  160. });
  161. function tab(e) {
  162. if (e.keyCode === 9) {
  163. e.preventDefault();
  164. }
  165. }
  166. const hideBubble = ref(false);
  167. const nodeType = computed<string | undefined>(() => {
  168. if (!editor.value) {
  169. return undefined;
  170. }
  171. const selection = editor.value.state.selection as any;
  172. const isImage = selection.node?.type.name === "image";
  173. const isVideo = selection.node?.type.name === "video";
  174. const isMagic = selection.node?.type.name === "magic";
  175. const isMath = selection.node?.type.name === "hb-math";
  176. const isCodeBlock = selection.$anchor.parent.type.name === "codeBlock";
  177. const isCell = selection instanceof CellSelection;
  178. const isTable = selection.node?.type.name === "table" || isCell; // 选中表格或者单元格
  179. const isText = selection instanceof TextSelection;
  180. if (isImage) return "image";
  181. if (isVideo) return "video";
  182. if (isTable) return "table";
  183. if (isMagic) return "magic";
  184. if (isMath) return "math";
  185. if (isCodeBlock) return "codeBlock";
  186. if (isText) return "text";
  187. return undefined;
  188. });
  189. watch(nodeType, () => {
  190. if (nodeType.value === "magic" || nodeType.value === "codeBlock") {
  191. hideBubble.value = true;
  192. } else {
  193. hideBubble.value = false;
  194. }
  195. });
  196. function getInstance() {
  197. return editor.value;
  198. }
  199. defineExpose({
  200. getInstance,
  201. });
  202. </script>
  203. <template>
  204. <div ref="editorBox" class="editor-background" :class="{ fullscreen: fullscreen }" @keydown="tab">
  205. <n-message-provider>
  206. <n-dialog-provider>
  207. <slot />
  208. <bubble-menu
  209. class="bubble-menu-bar"
  210. v-if="editor && editor.isEditable && bubbleMenu && nodeType"
  211. v-show="!hideBubble"
  212. :editor="editor"
  213. :tippy-options="{ duration: 0, maxWidth: 600, placement: 'top-start' }"
  214. >
  215. <slot name="bubble-menu" :nodeType="nodeType">
  216. <vivid-bubble-menu :node-type="nodeType" />
  217. </slot>
  218. </bubble-menu>
  219. <div
  220. class="editor"
  221. :class="{
  222. fullscreen: fullscreen,
  223. focus: isFocused && !fullscreen,
  224. online: page,
  225. }"
  226. spellcheck="false"
  227. >
  228. <div :class="{ 'editor-readonly': readonly }" style="width: 100%">
  229. <slot name="menu" :readonly="readonly">
  230. <vivid-menu class="editor-header" :editor="editor" :handleImageUpload="handleImageUpload" :handleVideoUpload="handleVideoUpload" :handlePackageUpload="handlePackageUpload"/>
  231. </slot>
  232. </div>
  233. <div class="editor-page" v-if="page">
  234. <editor-content
  235. class="editor-body editor-body-page markdown-body"
  236. :class="{ dark: props.dark, light: !props.dark }"
  237. :editor="editor"
  238. />
  239. </div>
  240. <editor-content
  241. v-else
  242. class="editor-body editor-body-flow markdown-body"
  243. :class="{ dark: props.dark, light: !props.dark }"
  244. :editor="editor"
  245. />
  246. <slot name="footer" :data="{ words, characters }">
  247. <vivid-footer :words="words" :characters="characters" />
  248. </slot>
  249. </div>
  250. </n-dialog-provider>
  251. </n-message-provider>
  252. </div>
  253. </template>
  254. <style scoped>
  255. .editor {
  256. border: 1px solid v-bind(vars.borderColor);
  257. border-radius: 3px;
  258. display: flex;
  259. flex-direction: column;
  260. box-sizing: border-box;
  261. height: 100%;
  262. transition-property: border-color, box-shadow;
  263. transition-duration: 0.2s;
  264. background: v-bind(vars.inputColor);
  265. position: relative;
  266. }
  267. .bubble-menu-bar {
  268. background: v-bind(vars.popoverColor);
  269. box-shadow: v-bind(vars.boxShadow2);
  270. border-radius: 10px;
  271. overflow: hidden;
  272. }
  273. .editor-background {
  274. width: 100%;
  275. height: 100%;
  276. }
  277. .editor-background.fullscreen {
  278. position: fixed;
  279. inset: 0;
  280. z-index: 100;
  281. margin: 0;
  282. padding: 0;
  283. background: v-bind(vars.cardColor);
  284. }
  285. .editor.fullscreen {
  286. position: fixed;
  287. inset: 0;
  288. z-index: 1;
  289. margin: 0;
  290. padding: 0;
  291. }
  292. .editor.fullscreen:hover {
  293. border-color: rgba(0, 0, 0, 0) !important;
  294. }
  295. .editor-header {
  296. border-bottom: 1px solid #cccccc;
  297. box-sizing: border-box;
  298. }
  299. .editor-page {
  300. box-sizing: border-box;
  301. flex: 1 1 auto;
  302. overflow: auto;
  303. -webkit-overflow-scrolling: touch;
  304. padding: 40px;
  305. width: 100%;
  306. display: flex;
  307. justify-content: center;
  308. background: v-bind(vars.baseColor);
  309. position: relative;
  310. }
  311. .editor-body {
  312. position: relative;
  313. }
  314. .editor-body-flow {
  315. box-sizing: border-box;
  316. -webkit-overflow-scrolling: touch;
  317. flex: 1 1 auto;
  318. overflow: auto;
  319. padding: 40px 0;
  320. position: relative;
  321. }
  322. .editor-body-page {
  323. box-sizing: border-box;
  324. -webkit-overflow-scrolling: touch;
  325. padding: 40px 0;
  326. height: fit-content;
  327. min-height: 297mm;
  328. border-radius: 10px;
  329. box-shadow: v-bind(vars.boxShadow3);
  330. overflow: hidden;
  331. max-width: 210mm;
  332. width: 100%;
  333. }
  334. .editor-body::v-deep(.is-editor-empty) {
  335. height: 100%;
  336. }
  337. .editor-body::v-deep(.tiptap) {
  338. height: 100%;
  339. }
  340. .editor-body-page::v-deep(.is-editor-empty) {
  341. height: 100%;
  342. }
  343. .editor-body-page::v-deep(.tiptap) {
  344. height: 100%;
  345. min-height: inherit;
  346. padding-top: 40px;
  347. padding-bottom: 40px;
  348. }
  349. ::v-deep(.tippy-box) {
  350. background-color: transparent;
  351. }
  352. ::v-deep(.tippy-arrow) {
  353. color: transparent;
  354. }
  355. .editor.online {
  356. align-items: center;
  357. overflow: hidden;
  358. }
  359. .editor-readonly {
  360. pointer-events: none;
  361. opacity: 0.6;
  362. }
  363. </style>
  364. <style>
  365. .tiptap > * {
  366. margin-left: 100px !important;
  367. margin-right: 100px !important;
  368. }
  369. .tiptap > section {
  370. }
  371. .tiptap > section:hover::after {
  372. content: " ";
  373. position: absolute;
  374. inset: 0;
  375. box-sizing: border-box;
  376. z-index: 1;
  377. border: 1px dashed;
  378. color: #00bd63;
  379. pointer-events: none;
  380. }
  381. .tiptap > section:hover {
  382. position: relative;
  383. }
  384. /* Give a remote user a caret */
  385. .collaboration-cursor__caret {
  386. position: relative;
  387. margin-left: -1px;
  388. margin-right: -1px;
  389. border-left: 1px solid #0d0d0d;
  390. border-right: 1px solid #0d0d0d;
  391. word-break: normal;
  392. pointer-events: none;
  393. }
  394. /* Render the username above the caret */
  395. .collaboration-cursor__label {
  396. position: absolute;
  397. top: -1.4em;
  398. left: -1px;
  399. font-size: 12px;
  400. font-style: normal;
  401. font-weight: 600;
  402. line-height: normal;
  403. user-select: none;
  404. color: #0d0d0d;
  405. padding: 0.1rem 0.3rem;
  406. border-radius: 3px 3px 3px 0;
  407. white-space: nowrap;
  408. }
  409. </style>