TextStylePanel.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <template>
  2. <div class="text-style-panel">
  3. <div class="preset-style">
  4. <div
  5. class="preset-style-item"
  6. v-for="item in presetStyles"
  7. :key="item.label"
  8. :style="item.style"
  9. @click="emitBatchRichTextCommand(item.cmd)"
  10. >{{item.label}}</div>
  11. </div>
  12. <Divider />
  13. <InputGroup compact class="row">
  14. <Select
  15. style="flex: 3;"
  16. :value="richTextAttrs.fontname"
  17. @change="value => emitRichTextCommand('fontname', value)"
  18. >
  19. <template #suffixIcon><IconFontSize /></template>
  20. <SelectOption v-for="font in availableFonts" :key="font.en" :value="font.en">
  21. <span :style="{ fontFamily: font.en }">{{font.zh}}</span>
  22. </SelectOption>
  23. </Select>
  24. <Select
  25. style="flex: 2;"
  26. :value="richTextAttrs.fontsize"
  27. @change="value => emitRichTextCommand('fontsize', value)"
  28. >
  29. <template #suffixIcon><IconAddText /></template>
  30. <SelectOption v-for="fontsize in fontSizeOptions" :key="fontsize" :value="fontsize">
  31. {{fontsize}}
  32. </SelectOption>
  33. </Select>
  34. </InputGroup>
  35. <ButtonGroup class="row">
  36. <Popover trigger="click">
  37. <template #content>
  38. <ColorPicker
  39. :modelValue="richTextAttrs.color"
  40. @update:modelValue="value => emitRichTextCommand('color', value)"
  41. />
  42. </template>
  43. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="文字颜色">
  44. <Button class="text-color-btn" style="flex: 1;">
  45. <IconText />
  46. <div class="text-color-block" :style="{ backgroundColor: richTextAttrs.color }"></div>
  47. </Button>
  48. </Tooltip>
  49. </Popover>
  50. <Popover trigger="click">
  51. <template #content>
  52. <ColorPicker
  53. :modelValue="richTextAttrs.backcolor"
  54. @update:modelValue="value => emitRichTextCommand('backcolor', value)"
  55. />
  56. </template>
  57. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="文字高亮">
  58. <Button class="text-color-btn" style="flex: 1;">
  59. <IconBackgroundColor />
  60. <div class="text-color-block" :style="{ backgroundColor: richTextAttrs.backcolor }"></div>
  61. </Button>
  62. </Tooltip>
  63. </Popover>
  64. <Popover trigger="click">
  65. <template #content>
  66. <ColorPicker
  67. :modelValue="fill"
  68. @update:modelValue="value => updateFill(value)"
  69. />
  70. </template>
  71. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="文本框填充">
  72. <Button class="text-color-btn" style="flex: 1;">
  73. <IconFill />
  74. <div class="text-color-block" :style="{ backgroundColor: fill }"></div>
  75. </Button>
  76. </Tooltip>
  77. </Popover>
  78. </ButtonGroup>
  79. <CheckboxButtonGroup class="row">
  80. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="加粗">
  81. <CheckboxButton
  82. style="flex: 1;"
  83. :checked="richTextAttrs.bold"
  84. @click="emitRichTextCommand('bold')"
  85. ><IconTextBold /></CheckboxButton>
  86. </Tooltip>
  87. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="斜体">
  88. <CheckboxButton
  89. style="flex: 1;"
  90. :checked="richTextAttrs.em"
  91. @click="emitRichTextCommand('em')"
  92. ><IconTextItalic /></CheckboxButton>
  93. </Tooltip>
  94. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="下划线">
  95. <CheckboxButton
  96. style="flex: 1;"
  97. :checked="richTextAttrs.underline"
  98. @click="emitRichTextCommand('underline')"
  99. ><IconTextUnderline /></CheckboxButton>
  100. </Tooltip>
  101. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="删除线">
  102. <CheckboxButton
  103. style="flex: 1;"
  104. :checked="richTextAttrs.strikethrough"
  105. @click="emitRichTextCommand('strikethrough')"
  106. ><IconStrikethrough /></CheckboxButton>
  107. </Tooltip>
  108. </CheckboxButtonGroup>
  109. <CheckboxButtonGroup class="row">
  110. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="上标">
  111. <CheckboxButton
  112. style="flex: 1;"
  113. :checked="richTextAttrs.superscript"
  114. @click="emitRichTextCommand('superscript')"
  115. ><IconUpOne /></CheckboxButton>
  116. </Tooltip>
  117. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="下标">
  118. <CheckboxButton
  119. style="flex: 1;"
  120. :checked="richTextAttrs.subscript"
  121. @click="emitRichTextCommand('subscript')"
  122. ><IconDownOne /></CheckboxButton>
  123. </Tooltip>
  124. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="行内代码">
  125. <CheckboxButton
  126. style="flex: 1;"
  127. :checked="richTextAttrs.code"
  128. @click="emitRichTextCommand('code')"
  129. ><IconCode /></CheckboxButton>
  130. </Tooltip>
  131. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="引用">
  132. <CheckboxButton
  133. style="flex: 1;"
  134. :checked="richTextAttrs.blockquote"
  135. @click="emitRichTextCommand('blockquote')"
  136. ><IconQuote /></CheckboxButton>
  137. </Tooltip>
  138. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="清除格式">
  139. <CheckboxButton
  140. style="flex: 1;"
  141. @click="emitRichTextCommand('clear')"
  142. ><IconFormat /></CheckboxButton>
  143. </Tooltip>
  144. </CheckboxButtonGroup>
  145. <Divider />
  146. <RadioGroup
  147. class="row"
  148. button-style="solid"
  149. :value="richTextAttrs.align"
  150. @change="e => emitRichTextCommand('align', e.target.value)"
  151. >
  152. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="左对齐">
  153. <RadioButton value="left" style="flex: 1;"><IconAlignTextLeft /></RadioButton>
  154. </Tooltip>
  155. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="居中">
  156. <RadioButton value="center" style="flex: 1;"><IconAlignTextCenter /></RadioButton>
  157. </Tooltip>
  158. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="右对齐">
  159. <RadioButton value="right" style="flex: 1;"><IconAlignTextRight /></RadioButton>
  160. </Tooltip>
  161. </RadioGroup>
  162. <CheckboxButtonGroup class="row">
  163. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="项目符号">
  164. <CheckboxButton
  165. style="flex: 1;"
  166. :checked="richTextAttrs.bulletList"
  167. @click="emitRichTextCommand('bulletList')"
  168. ><IconList /></CheckboxButton>
  169. </Tooltip>
  170. <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="编号">
  171. <CheckboxButton
  172. style="flex: 1;"
  173. :checked="richTextAttrs.orderedList"
  174. @click="emitRichTextCommand('orderedList')"
  175. ><IconOrderedList /></CheckboxButton>
  176. </Tooltip>
  177. </CheckboxButtonGroup>
  178. <Divider />
  179. <div class="row">
  180. <div style="flex: 2;">行间距:</div>
  181. <Select style="flex: 3;" :value="lineHeight" @change="value => updateLineHeight(value)">
  182. <template #suffixIcon><IconRowHeight /></template>
  183. <SelectOption v-for="item in lineHeightOptions" :key="item" :value="item">{{item}}倍</SelectOption>
  184. </Select>
  185. </div>
  186. <div class="row">
  187. <div style="flex: 2;">字间距:</div>
  188. <Select style="flex: 3;" :value="wordSpace" @change="value => updateWordSpace(value)">
  189. <template #suffixIcon><IconFullwidth /></template>
  190. <SelectOption v-for="item in wordSpaceOptions" :key="item" :value="item">{{item}}px</SelectOption>
  191. </Select>
  192. </div>
  193. <Divider />
  194. <ElementOutline />
  195. <Divider />
  196. <ElementShadow />
  197. <Divider />
  198. <ElementOpacity />
  199. </div>
  200. </template>
  201. <script lang="ts">
  202. import { computed, defineComponent, onUnmounted, ref, watch } from 'vue'
  203. import { useStore } from 'vuex'
  204. import { MutationTypes, State } from '@/store'
  205. import { PPTTextElement } from '@/types/slides'
  206. import emitter, { EmitterEvents } from '@/utils/emitter'
  207. import { TextAttrs } from '@/prosemirror/utils'
  208. import useHistorySnapshot from '@/hooks/useHistorySnapshot'
  209. import ElementOpacity from '../common/ElementOpacity.vue'
  210. import ElementOutline from '../common/ElementOutline.vue'
  211. import ElementShadow from '../common/ElementShadow.vue'
  212. const presetStyles = [
  213. {
  214. label: '大标题',
  215. style: {
  216. fontSize: '30px',
  217. fontWeight: 700,
  218. },
  219. cmd: [
  220. { command: 'clear' },
  221. { command: 'fontsize', value: '48px' },
  222. { command: 'align', value: 'center' },
  223. { command: 'bold' },
  224. ],
  225. },
  226. {
  227. label: '小标题',
  228. style: {
  229. fontSize: '24px',
  230. fontWeight: 700,
  231. },
  232. cmd: [
  233. { command: 'clear' },
  234. { command: 'fontsize', value: '36px' },
  235. { command: 'align', value: 'center' },
  236. { command: 'bold' },
  237. ],
  238. },
  239. {
  240. label: '正文',
  241. style: {
  242. fontSize: '20px',
  243. },
  244. cmd: [
  245. { command: 'clear' },
  246. { command: 'fontsize', value: '20px' },
  247. ],
  248. },
  249. {
  250. label: '注释',
  251. style: {
  252. fontSize: '16px',
  253. fontStyle: 'italic',
  254. },
  255. cmd: [
  256. { command: 'clear' },
  257. { command: 'fontsize', value: '16px' },
  258. { command: 'em' },
  259. ],
  260. },
  261. ]
  262. interface CommandPayload {
  263. command: string;
  264. value?: string;
  265. }
  266. export default defineComponent({
  267. name: 'text-style-panel',
  268. components: {
  269. ElementOpacity,
  270. ElementOutline,
  271. ElementShadow,
  272. },
  273. setup() {
  274. const store = useStore<State>()
  275. const handleElement = computed<PPTTextElement>(() => store.getters.handleElement)
  276. const fill = ref<string>()
  277. const lineHeight = ref<number>()
  278. const wordSpace = ref<number>()
  279. watch(handleElement, () => {
  280. if(!handleElement.value || handleElement.value.type !== 'text') return
  281. fill.value = handleElement.value.fill || '#000'
  282. lineHeight.value = handleElement.value.lineHeight || 1.5
  283. wordSpace.value = handleElement.value.wordSpace || 0
  284. }, { deep: true, immediate: true })
  285. const richTextAttrs = ref<TextAttrs>({
  286. bold: false,
  287. em: false,
  288. underline: false,
  289. strikethrough: false,
  290. superscript: false,
  291. subscript: false,
  292. code: false,
  293. color: '#000',
  294. backcolor: '#000',
  295. fontsize: '12px',
  296. fontname: '微软雅黑',
  297. align: 'left',
  298. bulletList: false,
  299. orderedList: false,
  300. blockquote: false,
  301. })
  302. const availableFonts = computed(() => store.state.availableFonts)
  303. const fontSizeOptions = [
  304. '12px', '14px', '16px', '18px', '20px', '22px', '24px', '28px', '32px',
  305. '36px', '40px', '44px', '48px', '54px', '60px', '66px', '72px', '80px',
  306. ]
  307. const lineHeightOptions = [0.5, 1.0, 1.2, 1.5, 1.8, 2.0, 3.0]
  308. const wordSpaceOptions = [0, 1, 2, 3, 4, 5, 8]
  309. const updateRichTextAttrs = (attr: TextAttrs) => richTextAttrs.value = attr
  310. emitter.on(EmitterEvents.UPDATE_TEXT_STATE, attr => updateRichTextAttrs(attr))
  311. onUnmounted(() => {
  312. emitter.off(EmitterEvents.UPDATE_TEXT_STATE, attr => updateRichTextAttrs(attr))
  313. })
  314. const emitRichTextCommand = (command: string, value?: string) => {
  315. emitter.emit(EmitterEvents.EXEC_TEXT_COMMAND, { command, value })
  316. }
  317. const emitBatchRichTextCommand = (payload: CommandPayload[]) => {
  318. emitter.emit(EmitterEvents.EXEC_TEXT_COMMAND, payload)
  319. }
  320. const { addHistorySnapshot } = useHistorySnapshot()
  321. const updateLineHeight = (value: number) => {
  322. const props = { lineHeight: value }
  323. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  324. addHistorySnapshot()
  325. }
  326. const updateWordSpace = (value: number) => {
  327. const props = { wordSpace: value }
  328. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  329. addHistorySnapshot()
  330. }
  331. const updateFill = (value: string) => {
  332. const props = { fill: value }
  333. store.commit(MutationTypes.UPDATE_ELEMENT, { id: handleElement.value.id, props })
  334. addHistorySnapshot()
  335. }
  336. return {
  337. fill,
  338. lineHeight,
  339. wordSpace,
  340. richTextAttrs,
  341. availableFonts,
  342. fontSizeOptions,
  343. lineHeightOptions,
  344. wordSpaceOptions,
  345. updateLineHeight,
  346. updateWordSpace,
  347. updateFill,
  348. emitRichTextCommand,
  349. emitBatchRichTextCommand,
  350. presetStyles,
  351. }
  352. },
  353. })
  354. </script>
  355. <style lang="scss" scoped>
  356. .text-style-panel {
  357. user-select: none;
  358. }
  359. .row {
  360. width: 100%;
  361. display: flex;
  362. align-items: center;
  363. margin-bottom: 10px;
  364. }
  365. .preset-style {
  366. display: flex;
  367. flex-wrap: wrap;
  368. margin-bottom: 10px;
  369. }
  370. .preset-style-item {
  371. width: 50%;
  372. height: 60px;
  373. border: solid 1px #d6d6d6;
  374. box-sizing: border-box;
  375. display: flex;
  376. justify-content: center;
  377. align-items: center;
  378. position: relative;
  379. cursor: pointer;
  380. transition: all .2s;
  381. &:hover {
  382. border-color: $themeColor;
  383. color: $themeColor;
  384. z-index: 1;
  385. }
  386. &:nth-child(2n) {
  387. margin-left: -1px;
  388. }
  389. &:nth-child(n+3) {
  390. margin-top: -1px;
  391. }
  392. }
  393. .text-color-btn {
  394. display: flex;
  395. flex-direction: column;
  396. justify-content: center;
  397. align-items: center;
  398. }
  399. .text-color-block {
  400. width: 16px;
  401. height: 3px;
  402. margin-top: 1px;
  403. }
  404. </style>