main.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { customAlphabet } from 'nanoid'
  2. import { defineStore } from 'pinia'
  3. import { ToolbarStates } from '@/types/toolbar'
  4. import type { CreatingElement, ShapeFormatPainter, TextFormatPainter } from '@/types/edit'
  5. import type { DialogForExportTypes } from '@/types/export'
  6. import { type TextAttrs, defaultRichTextAttrs } from '@/utils/prosemirror/utils'
  7. import { useSlidesStore } from './slides'
  8. export interface MainState {
  9. activeElementIdList: string[]
  10. handleElementId: string
  11. activeGroupElementId: string
  12. hiddenElementIdList: string[]
  13. canvasPercentage: number
  14. canvasScale: number
  15. canvasDragged: boolean
  16. thumbnailsFocus: boolean
  17. editorAreaFocus: boolean
  18. disableHotkeys: boolean
  19. gridLineSize: number
  20. showRuler: boolean
  21. creatingElement: CreatingElement | null
  22. creatingCustomShape: boolean
  23. toolbarState: ToolbarStates
  24. clipingImageElementId: string
  25. isScaling: boolean
  26. richTextAttrs: TextAttrs
  27. selectedTableCells: string[]
  28. selectedSlidesIndex: number[]
  29. dialogForExport: DialogForExportTypes
  30. databaseId: string
  31. textFormatPainter: TextFormatPainter | null
  32. shapeFormatPainter: ShapeFormatPainter | null
  33. showSelectPanel: boolean
  34. showSearchPanel: boolean
  35. showNotesPanel: boolean
  36. showMarkupPanel: boolean
  37. showAIPPTDialog: boolean
  38. }
  39. const nanoid = customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
  40. export const databaseId = nanoid(10)
  41. export const useMainStore = defineStore('main', {
  42. state: (): MainState => ({
  43. activeElementIdList: [], // 被选中的元素ID集合,包含 handleElementId
  44. handleElementId: '', // 正在操作的元素ID
  45. activeGroupElementId: '', // 组合元素成员中,被选中可独立操作的元素ID
  46. hiddenElementIdList: [], // 被隐藏的元素ID集合
  47. canvasPercentage: 90, // 画布可视区域百分比
  48. canvasScale: 1, // 画布缩放比例(基于宽度{{slidesStore.viewportSize}}像素)
  49. canvasDragged: false, // 画布被拖拽移动
  50. thumbnailsFocus: false, // 左侧导航缩略图区域聚焦
  51. editorAreaFocus: false, // 编辑区域聚焦
  52. disableHotkeys: false, // 禁用快捷键
  53. gridLineSize: 0, // 网格线尺寸(0表示不显示网格线)
  54. showRuler: false, // 显示标尺
  55. creatingElement: null, // 正在插入的元素信息,需要通过绘制插入的元素(文字、形状、线条)
  56. creatingCustomShape: false, // 正在绘制任意多边形
  57. toolbarState: ToolbarStates.SLIDE_DESIGN, // 右侧工具栏状态
  58. clipingImageElementId: '', // 当前正在裁剪的图片ID
  59. richTextAttrs: defaultRichTextAttrs, // 富文本状态
  60. selectedTableCells: [], // 选中的表格单元格
  61. isScaling: true, // 正在进行元素缩放
  62. selectedSlidesIndex: [], // 当前被选中的页面索引集合
  63. dialogForExport: '', // 导出面板
  64. databaseId, // 标识当前应用的indexedDB数据库ID
  65. textFormatPainter: null, // 文字格式刷
  66. shapeFormatPainter: null, // 形状格式刷
  67. showSelectPanel: false, // 打开选择面板
  68. showSearchPanel: false, // 打开查找替换面板
  69. showNotesPanel: false, // 打开批注面板
  70. showMarkupPanel: false, // 打开类型标注面板
  71. showAIPPTDialog: false, // 打开AIPPT创建窗口
  72. }),
  73. getters: {
  74. activeElementList(state) {
  75. const slidesStore = useSlidesStore()
  76. const currentSlide = slidesStore.currentSlide
  77. if (!currentSlide || !currentSlide.elements) return []
  78. return currentSlide.elements.filter(element => state.activeElementIdList.includes(element.id))
  79. },
  80. handleElement(state) {
  81. const slidesStore = useSlidesStore()
  82. const currentSlide = slidesStore.currentSlide
  83. if (!currentSlide || !currentSlide.elements) return null
  84. return currentSlide.elements.find(element => state.handleElementId === element.id) || null
  85. },
  86. },
  87. actions: {
  88. setActiveElementIdList(activeElementIdList: string[]) {
  89. if (activeElementIdList.length === 1) this.handleElementId = activeElementIdList[0]
  90. else this.handleElementId = ''
  91. this.activeElementIdList = activeElementIdList
  92. },
  93. setHandleElementId(handleElementId: string) {
  94. this.handleElementId = handleElementId
  95. },
  96. setActiveGroupElementId(activeGroupElementId: string) {
  97. this.activeGroupElementId = activeGroupElementId
  98. },
  99. setHiddenElementIdList(hiddenElementIdList: string[]) {
  100. this.hiddenElementIdList = hiddenElementIdList
  101. },
  102. setCanvasPercentage(percentage: number) {
  103. this.canvasPercentage = percentage
  104. },
  105. setCanvasScale(scale: number) {
  106. this.canvasScale = scale
  107. },
  108. setCanvasDragged(isDragged: boolean) {
  109. this.canvasDragged = isDragged
  110. },
  111. setThumbnailsFocus(isFocus: boolean) {
  112. this.thumbnailsFocus = isFocus
  113. },
  114. setEditorareaFocus(isFocus: boolean) {
  115. this.editorAreaFocus = isFocus
  116. },
  117. setDisableHotkeysState(disable: boolean) {
  118. this.disableHotkeys = disable
  119. },
  120. setGridLineSize(size: number) {
  121. this.gridLineSize = size
  122. },
  123. setRulerState(show: boolean) {
  124. this.showRuler = show
  125. },
  126. setCreatingElement(element: CreatingElement | null) {
  127. this.creatingElement = element
  128. },
  129. setCreatingCustomShapeState(state: boolean) {
  130. this.creatingCustomShape = state
  131. },
  132. setToolbarState(toolbarState: ToolbarStates) {
  133. this.toolbarState = toolbarState
  134. },
  135. setClipingImageElementId(elId: string) {
  136. this.clipingImageElementId = elId
  137. },
  138. setRichtextAttrs(attrs: TextAttrs) {
  139. this.richTextAttrs = attrs
  140. },
  141. setSelectedTableCells(cells: string[]) {
  142. this.selectedTableCells = cells
  143. },
  144. setScalingState(isScaling: boolean) {
  145. this.isScaling = isScaling
  146. },
  147. updateSelectedSlidesIndex(selectedSlidesIndex: number[]) {
  148. this.selectedSlidesIndex = selectedSlidesIndex
  149. },
  150. setDialogForExport(type: DialogForExportTypes) {
  151. this.dialogForExport = type
  152. },
  153. setTextFormatPainter(textFormatPainter: TextFormatPainter | null) {
  154. this.textFormatPainter = textFormatPainter
  155. },
  156. setShapeFormatPainter(shapeFormatPainter: ShapeFormatPainter | null) {
  157. this.shapeFormatPainter = shapeFormatPainter
  158. },
  159. setSelectPanelState(show: boolean) {
  160. this.showSelectPanel = show
  161. },
  162. setSearchPanelState(show: boolean) {
  163. this.showSearchPanel = show
  164. },
  165. setNotesPanelState(show: boolean) {
  166. this.showNotesPanel = show
  167. },
  168. setMarkupPanelState(show: boolean) {
  169. this.showMarkupPanel = show
  170. },
  171. setAIPPTDialogState(show: boolean) {
  172. this.showAIPPTDialog = show
  173. },
  174. },
  175. })