sp-editor.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. <template>
  2. <view class="sp-editor" :style="{ '--icon-size': iconSize, '--icon-columns': iconColumns }">
  3. <view class="sp-editor-toolbar" v-if="!readOnly" @tap="format">
  4. <!-- 标题栏 -->
  5. <fab-tool v-if="toolbarList.includes('header')" :visible="curFab == 'header'">
  6. <view
  7. :class="formats.header ? 'ql-active' : ''"
  8. class="iconfont icon-header"
  9. title="标题"
  10. data-name="header"
  11. @click.stop="fabTap('header')"
  12. ></view>
  13. <template #content>
  14. <view class="fab-tools" @click.stop="fabTapSub($event, 'header')">
  15. <view v-for="item in fabTools.header" :key="item.value">
  16. <view
  17. v-if="toolbarList.includes(item.name)"
  18. class="fab-sub"
  19. :class="[formats.header === item.value ? 'ql-active' : '', item.icon ? 'iconfont' : '', item.icon]"
  20. :title="item.title"
  21. data-name="header"
  22. :data-value="item.value"
  23. ></view>
  24. </view>
  25. </view>
  26. </template>
  27. </fab-tool>
  28. <view
  29. v-if="toolbarList.includes('bold')"
  30. :class="formats.bold ? 'ql-active' : ''"
  31. class="iconfont icon-zitijiacu"
  32. title="加粗"
  33. data-name="bold"
  34. ></view>
  35. <view
  36. v-if="toolbarList.includes('italic')"
  37. :class="formats.italic ? 'ql-active' : ''"
  38. class="iconfont icon-zitixieti"
  39. title="斜体"
  40. data-name="italic"
  41. ></view>
  42. <view
  43. v-if="toolbarList.includes('underline')"
  44. :class="formats.underline ? 'ql-active' : ''"
  45. class="iconfont icon-zitixiahuaxian"
  46. title="下划线"
  47. data-name="underline"
  48. ></view>
  49. <view
  50. v-if="toolbarList.includes('strike')"
  51. :class="formats.strike ? 'ql-active' : ''"
  52. class="iconfont icon-zitishanchuxian"
  53. title="删除线"
  54. data-name="strike"
  55. ></view>
  56. <!-- 对齐方式 -->
  57. <fab-tool v-if="toolbarList.includes('align')" :visible="curFab == 'align'">
  58. <view
  59. :class="formats.align ? 'ql-active' : ''"
  60. class="iconfont icon-zuoyouduiqi"
  61. title="对齐方式"
  62. data-name="align"
  63. @click.stop="fabTap('align')"
  64. ></view>
  65. <template #content>
  66. <view class="fab-tools" @click.stop="fabTapSub($event, 'align')">
  67. <view v-for="item in fabTools.align" :key="item.value">
  68. <view
  69. v-if="toolbarList.includes(item.name)"
  70. class="fab-sub"
  71. :class="[formats.align === item.value ? 'ql-active' : '', item.icon ? 'iconfont' : '', item.icon]"
  72. :title="item.title"
  73. data-name="align"
  74. :data-value="item.value"
  75. ></view>
  76. </view>
  77. </view>
  78. </template>
  79. </fab-tool>
  80. <!-- 行间距 -->
  81. <fab-tool v-if="toolbarList.includes('lineHeight')" :visible="curFab == 'lineHeight'">
  82. <view
  83. :class="formats.lineHeight ? 'ql-active' : ''"
  84. class="iconfont icon-line-height"
  85. title="行间距"
  86. data-name="lineHeight"
  87. @click.stop="fabTap('lineHeight')"
  88. ></view>
  89. <template #content>
  90. <view class="fab-tools" @click.stop="fabTapSub($event, 'lineHeight')">
  91. <view v-for="item in fabTools.lineHeight" :key="item.value">
  92. <view
  93. class="fab-sub"
  94. :class="[formats.lineHeight === item.value ? 'ql-active' : '', item.icon ? 'iconfont' : '', item.icon]"
  95. :title="item.title"
  96. data-name="lineHeight"
  97. :data-value="item.value"
  98. >
  99. {{ item.name }}
  100. </view>
  101. </view>
  102. </view>
  103. </template>
  104. </fab-tool>
  105. <!-- 字间距 -->
  106. <fab-tool v-if="toolbarList.includes('letterSpacing')" :visible="curFab == 'letterSpacing'">
  107. <view
  108. :class="formats.letterSpacing ? 'ql-active' : ''"
  109. class="iconfont icon-Character-Spacing"
  110. title="字间距"
  111. data-name="letterSpacing"
  112. @click.stop="fabTap('letterSpacing')"
  113. ></view>
  114. <template #content>
  115. <view class="fab-tools" @click.stop="fabTapSub($event, 'letterSpacing')">
  116. <view v-for="item in fabTools.space" :key="item.value">
  117. <view
  118. class="fab-sub"
  119. :class="[
  120. formats.letterSpacing === item.value ? 'ql-active' : '',
  121. item.icon ? 'iconfont' : '',
  122. item.icon
  123. ]"
  124. :title="item.title"
  125. data-name="letterSpacing"
  126. :data-value="item.value"
  127. >
  128. {{ item.name }}
  129. </view>
  130. </view>
  131. </view>
  132. </template>
  133. </fab-tool>
  134. <!-- 段前距 -->
  135. <fab-tool v-if="toolbarList.includes('marginTop')" :visible="curFab == 'marginTop'">
  136. <view
  137. :class="formats.marginTop ? 'ql-active' : ''"
  138. class="iconfont icon-722bianjiqi_duanqianju"
  139. title="段前距"
  140. data-name="marginTop"
  141. @click.stop="fabTap('marginTop')"
  142. ></view>
  143. <template #content>
  144. <view class="fab-tools" @click.stop="fabTapSub($event, 'marginTop')">
  145. <view v-for="item in fabTools.space" :key="item.value">
  146. <view
  147. class="fab-sub"
  148. :class="[formats.marginTop === item.value ? 'ql-active' : '', item.icon ? 'iconfont' : '', item.icon]"
  149. :title="item.title"
  150. data-name="marginTop"
  151. :data-value="item.value"
  152. >
  153. {{ item.name }}
  154. </view>
  155. </view>
  156. </view>
  157. </template>
  158. </fab-tool>
  159. <!-- 段后距 -->
  160. <fab-tool v-if="toolbarList.includes('marginBottom')" :visible="curFab == 'marginBottom'">
  161. <view
  162. :class="formats.marginBottom ? 'ql-active' : ''"
  163. class="iconfont icon-723bianjiqi_duanhouju"
  164. title="段后距"
  165. data-name="marginBottom"
  166. @click.stop="fabTap('marginBottom')"
  167. ></view>
  168. <template #content>
  169. <view class="fab-tools" @click.stop="fabTapSub($event, 'marginBottom')">
  170. <view v-for="item in fabTools.space" :key="item.value">
  171. <view
  172. class="fab-sub"
  173. :class="[
  174. formats.marginBottom === item.value ? 'ql-active' : '',
  175. item.icon ? 'iconfont' : '',
  176. item.icon
  177. ]"
  178. :title="item.title"
  179. data-name="marginBottom"
  180. :data-value="item.value"
  181. >
  182. {{ item.name }}
  183. </view>
  184. </view>
  185. </view>
  186. </template>
  187. </fab-tool>
  188. <!-- 字体栏 -->
  189. <fab-tool v-if="toolbarList.includes('fontFamily')" :visible="curFab == 'fontFamily'">
  190. <view
  191. :class="formats.fontFamily ? 'ql-active' : ''"
  192. class="iconfont icon-font"
  193. title="字体"
  194. data-name="fontFamily"
  195. @click.stop="fabTap('fontFamily')"
  196. ></view>
  197. <template #content>
  198. <view class="fab-tools" @click.stop="fabTapSub($event, 'fontFamily')">
  199. <view v-for="item in fabTools.fontFamily" :key="item.value">
  200. <view
  201. class="fab-sub"
  202. :class="[formats.fontFamily === item.value ? 'ql-active' : '', item.icon ? 'iconfont' : '', item.icon]"
  203. :title="item.title"
  204. data-name="fontFamily"
  205. :data-value="item.value"
  206. >
  207. {{ item.name }}
  208. </view>
  209. </view>
  210. </view>
  211. </template>
  212. </fab-tool>
  213. <!-- 字体大小栏 -->
  214. <fab-tool v-if="toolbarList.includes('fontSize')" :visible="curFab == 'fontSize'">
  215. <view
  216. :class="formats.fontSize ? 'ql-active' : ''"
  217. class="iconfont icon-fontsize"
  218. title="字号"
  219. data-name="fontSize"
  220. @click.stop="fabTap('fontSize')"
  221. ></view>
  222. <template #content>
  223. <view class="fab-tools" @click.stop="fabTapSub($event, 'fontSize')">
  224. <view v-for="item in fabTools.fontSize" :key="item.value">
  225. <view
  226. class="fab-sub"
  227. :class="[formats.fontSize === item.value ? 'ql-active' : '', item.icon ? 'iconfont' : '', item.icon]"
  228. :title="item.title"
  229. data-name="fontSize"
  230. :data-value="item.value"
  231. >
  232. {{ item.name }}
  233. </view>
  234. </view>
  235. </view>
  236. </template>
  237. </fab-tool>
  238. <view
  239. v-if="toolbarList.includes('color')"
  240. :style="{ color: formats.color ? textColor : 'initial' }"
  241. class="iconfont icon-text_color"
  242. title="文字颜色"
  243. data-name="color"
  244. :data-value="textColor"
  245. ></view>
  246. <view
  247. v-if="toolbarList.includes('backgroundColor')"
  248. :style="{ color: formats.backgroundColor ? backgroundColor : 'initial' }"
  249. class="iconfont icon-fontbgcolor"
  250. title="背景颜色"
  251. data-name="backgroundColor"
  252. :data-value="backgroundColor"
  253. ></view>
  254. <view v-if="toolbarList.includes('date')" class="iconfont icon-date" title="日期" @tap="insertDate"></view>
  255. <view
  256. v-if="toolbarList.includes('listCheck')"
  257. class="iconfont icon--checklist"
  258. title="待办"
  259. data-name="list"
  260. data-value="check"
  261. ></view>
  262. <view
  263. v-if="toolbarList.includes('listOrdered')"
  264. :class="formats.list === 'ordered' ? 'ql-active' : ''"
  265. class="iconfont icon-youxupailie"
  266. title="有序列表"
  267. data-name="list"
  268. data-value="ordered"
  269. ></view>
  270. <view
  271. v-if="toolbarList.includes('listBullet')"
  272. :class="formats.list === 'bullet' ? 'ql-active' : ''"
  273. class="iconfont icon-wuxupailie"
  274. title="无序列表"
  275. data-name="list"
  276. data-value="bullet"
  277. ></view>
  278. <view
  279. v-if="toolbarList.includes('divider')"
  280. class="iconfont icon-fengexian"
  281. title="分割线"
  282. @click="insertDivider"
  283. ></view>
  284. <view
  285. v-if="toolbarList.includes('indentDec')"
  286. class="iconfont icon-outdent"
  287. title="减少缩进"
  288. data-name="indent"
  289. data-value="-1"
  290. ></view>
  291. <view
  292. v-if="toolbarList.includes('indentInc')"
  293. class="iconfont icon-indent"
  294. title="增加缩进"
  295. data-name="indent"
  296. data-value="+1"
  297. ></view>
  298. <view
  299. v-if="toolbarList.includes('scriptSub')"
  300. :class="formats.script === 'sub' ? 'ql-active' : ''"
  301. class="iconfont icon-zitixiabiao"
  302. title="下标"
  303. data-name="script"
  304. data-value="sub"
  305. ></view>
  306. <view
  307. v-if="toolbarList.includes('scriptSuper')"
  308. :class="formats.script === 'super' ? 'ql-active' : ''"
  309. class="iconfont icon-zitishangbiao"
  310. title="上标"
  311. data-name="script"
  312. data-value="super"
  313. ></view>
  314. <view
  315. v-if="toolbarList.includes('direction')"
  316. :class="formats.direction === 'rtl' ? 'ql-active' : ''"
  317. class="iconfont icon-direction-rtl"
  318. title="文本方向"
  319. data-name="direction"
  320. data-value="rtl"
  321. ></view>
  322. <view
  323. v-if="toolbarList.includes('image')"
  324. class="iconfont icon-charutupian"
  325. title="图片"
  326. @tap="insertImage"
  327. ></view>
  328. <view v-if="toolbarList.includes('video')" class="iconfont icon-video" title="视频" @tap="insertVideo"></view>
  329. <view
  330. v-if="toolbarList.includes('link')"
  331. class="iconfont icon-charulianjie"
  332. title="超链接"
  333. @tap="insertLink"
  334. ></view>
  335. <view v-if="toolbarList.includes('undo')" class="iconfont icon-undo" title="撤销" @tap="undo"></view>
  336. <view v-if="toolbarList.includes('redo')" class="iconfont icon-redo" title="重做" @tap="redo"></view>
  337. <view
  338. v-if="toolbarList.includes('removeFormat')"
  339. class="iconfont icon-clearedformat"
  340. title="清除格式"
  341. @tap="removeFormat"
  342. ></view>
  343. <view v-if="toolbarList.includes('clear')" class="iconfont icon-shanchu" title="清空" @tap="clear"></view>
  344. <view v-if="toolbarList.includes('export')" class="iconfont icon-baocun" title="导出" @tap="exportHtml"></view>
  345. </view>
  346. <!-- 自定义功能组件 -->
  347. <!-- 调色板 -->
  348. <color-picker
  349. v-if="toolbarList.includes('color') || toolbarList.includes('backgroundColor')"
  350. ref="colorPickerRef"
  351. :color="defaultColor"
  352. @confirm="confirmColor"
  353. ></color-picker>
  354. <!-- 添加链接的操作弹窗 -->
  355. <link-edit v-if="toolbarList.includes('link') && !readOnly" ref="linkEditRef" @confirm="confirmLink"></link-edit>
  356. <view class="sp-editor-wrapper" @longpress="eLongpress">
  357. <editor
  358. :id="editorId"
  359. class="ql-editor editor-container"
  360. :class="{ 'ql-image-overlay-none': readOnly }"
  361. show-img-size
  362. show-img-toolbar
  363. show-img-resize
  364. :placeholder="placeholder"
  365. :read-only="readOnly"
  366. @statuschange="onStatusChange"
  367. @ready="onEditorReady"
  368. @input="onEditorInput"
  369. ></editor>
  370. </view>
  371. </view>
  372. </template>
  373. <script>
  374. import ColorPicker from './color-picker.vue'
  375. import LinkEdit from './link-edit.vue'
  376. import FabTool from './fab-tool.vue'
  377. import { addLink, linkFlag } from '../../utils'
  378. export default {
  379. components: {
  380. ColorPicker,
  381. LinkEdit,
  382. FabTool
  383. },
  384. props: {
  385. // 编辑器id可传入,以便循环组件使用,防止id重复
  386. editorId: {
  387. type: String,
  388. default: 'editor'
  389. },
  390. placeholder: {
  391. type: String,
  392. default: '写点什么吧 ~'
  393. },
  394. // 是否只读
  395. readOnly: {
  396. type: Boolean,
  397. default: false
  398. },
  399. // 最大字数限制,-1不限
  400. maxlength: {
  401. type: Number,
  402. default: -1
  403. },
  404. // 工具栏配置
  405. toolbarConfig: {
  406. type: Object,
  407. default: () => {
  408. return {
  409. keys: [], // 要显示的工具,优先级最大
  410. excludeKeys: [], // 除这些指定的工具外,其他都显示
  411. iconSize: '18px', // 工具栏字体大小
  412. iconColumns: 10 // 工具栏列数
  413. }
  414. }
  415. }
  416. },
  417. watch: {
  418. toolbarConfig: {
  419. deep: true,
  420. immediate: true,
  421. handler(newToolbar) {
  422. /**
  423. * 若工具栏配置中keys存在,则以keys为准
  424. * 否则以excludeKeys向toolbarAllList中排查
  425. * 若keys与excludeKeys皆为空,则以toolbarAllList为准
  426. */
  427. if (newToolbar.keys?.length > 0) {
  428. this.toolbarList = newToolbar.keys
  429. } else {
  430. this.toolbarList =
  431. newToolbar.excludeKeys?.length > 0
  432. ? this.toolbarAllList.filter((item) => !newToolbar.excludeKeys.includes(item))
  433. : this.toolbarAllList
  434. }
  435. this.iconSize = newToolbar.iconSize || '18px'
  436. this.iconColumns = newToolbar.iconColumns || 10
  437. }
  438. }
  439. },
  440. data() {
  441. return {
  442. formats: {},
  443. curFab: '', // 当前悬浮工具栏
  444. fabXY: {},
  445. textColor: '',
  446. backgroundColor: '',
  447. curColor: '',
  448. defaultColor: { r: 0, g: 0, b: 0, a: 1 }, // 调色板默认颜色
  449. iconSize: '20px', // 工具栏图标字体大小
  450. iconColumns: 10, // 工具栏列数
  451. toolbarList: [],
  452. toolbarAllList: [
  453. 'header', // 标题
  454. 'H1', // 一级标题
  455. 'H2', // 二级标题
  456. 'H3', // 三级标题
  457. 'H4', // 四级标题
  458. 'H5', // 五级标题
  459. 'H6', // 六级标题
  460. 'bold', // 加粗
  461. 'italic', // 斜体
  462. 'underline', // 下划线
  463. 'strike', // 删除线
  464. 'align', // 对齐方式
  465. 'alignLeft', // 左对齐
  466. 'alignCenter', // 居中对齐
  467. 'alignRight', // 右对齐
  468. 'alignJustify', // 两端对齐
  469. 'lineHeight', // 行间距
  470. 'letterSpacing', // 字间距
  471. 'marginTop', // 段前距
  472. 'marginBottom', // 段后距
  473. 'fontFamily', // 字体
  474. 'fontSize', // 字号
  475. 'color', // 文字颜色
  476. 'backgroundColor', // 背景颜色
  477. 'date', // 日期
  478. 'listCheck', // 待办
  479. 'listOrdered', // 有序列表
  480. 'listBullet', // 无序列表
  481. 'indentInc', // 增加缩进
  482. 'indentDec', // 减少缩进
  483. 'divider', // 分割线
  484. 'scriptSub', // 下标
  485. 'scriptSuper', // 上标
  486. 'direction', // 文本方向
  487. 'image', // 图片
  488. 'video', // 视频
  489. 'link', // 超链接
  490. 'undo', // 撤销
  491. 'redo', // 重做
  492. 'removeFormat', // 清除格式
  493. 'clear', // 清空
  494. 'export' // 导出
  495. ],
  496. fabTools: {
  497. header: [
  498. { title: '一级标题', name: 'H1', value: 1, icon: 'icon-format-header-1' },
  499. { title: '二级标题', name: 'H2', value: 2, icon: 'icon-format-header-2' },
  500. { title: '三级标题', name: 'H3', value: 3, icon: 'icon-format-header-3' },
  501. { title: '四级标题', name: 'H4', value: 4, icon: 'icon-format-header-4' },
  502. { title: '五级标题', name: 'H5', value: 5, icon: 'icon-format-header-5' },
  503. { title: '六级标题', name: 'H6', value: 6, icon: 'icon-format-header-6' }
  504. ],
  505. fontFamily: [
  506. { title: '宋体', name: '宋', value: '宋体', icon: '' },
  507. { title: '黑体', name: '黑', value: '黑体', icon: '' },
  508. { title: '楷体', name: '楷', value: '楷体', icon: '' },
  509. { title: '仿宋', name: '仿', value: '仿宋', icon: '' },
  510. { title: '华文隶书', name: '隶', value: 'STLiti', icon: '' },
  511. { title: '华文行楷', name: '行', value: 'STXingkai', icon: '' },
  512. { title: '幼圆', name: '圆', value: 'YouYuan', icon: '' }
  513. ],
  514. fontSize: [
  515. { title: '12', name: '12', value: '12px', icon: '' },
  516. { title: '14', name: '14', value: '14px', icon: '' },
  517. { title: '16', name: '16', value: '16px', icon: '' },
  518. { title: '18', name: '18', value: '18px', icon: '' },
  519. { title: '20', name: '20', value: '20px', icon: '' },
  520. { title: '22', name: '22', value: '22px', icon: '' },
  521. { title: '24', name: '24', value: '24px', icon: '' }
  522. ],
  523. align: [
  524. { title: '左对齐', name: 'alignLeft', value: 'left', icon: 'icon-zuoduiqi' },
  525. { title: '居中对齐', name: 'alignCenter', value: 'center', icon: 'icon-juzhongduiqi' },
  526. { title: '右对齐', name: 'alignRight', value: 'right', icon: 'icon-youduiqi' },
  527. { title: '两端对齐', name: 'alignJustify', value: 'justify', icon: 'icon-zuoyouduiqi' }
  528. ],
  529. lineHeight: [
  530. { title: '1倍', name: '1', value: '1', icon: '' },
  531. { title: '1.5倍', name: '1.5', value: '1.5', icon: '' },
  532. { title: '2倍', name: '2', value: '2', icon: '' },
  533. { title: '2.5倍', name: '2.5', value: '2.5', icon: '' },
  534. { title: '3倍', name: '3', value: '3', icon: '' }
  535. ],
  536. // 字间距/段前距/段后距
  537. space: [
  538. { title: '0.5倍', name: '0.5', value: '0.5em', icon: '' },
  539. { title: '1倍', name: '1', value: '1em', icon: '' },
  540. { title: '1.5倍', name: '1.5', value: '1.5em', icon: '' },
  541. { title: '2倍', name: '2', value: '2em', icon: '' },
  542. { title: '2.5倍', name: '2.5', value: '2.5em', icon: '' },
  543. { title: '3倍', name: '3', value: '3em', icon: '' }
  544. ]
  545. }
  546. }
  547. },
  548. methods: {
  549. onEditorReady() {
  550. uni
  551. .createSelectorQuery()
  552. .in(this)
  553. .select('#' + this.editorId)
  554. .context((res) => {
  555. this.editorCtx = res.context
  556. this.$emit('init', this.editorCtx, this.editorId)
  557. })
  558. .exec()
  559. },
  560. undo() {
  561. this.editorCtx.undo()
  562. },
  563. redo() {
  564. this.editorCtx.redo()
  565. },
  566. format(e) {
  567. let { name, value } = e.target.dataset
  568. if (!name) return
  569. switch (name) {
  570. case 'color':
  571. case 'backgroundColor':
  572. this.curColor = name
  573. this.showPicker()
  574. break
  575. default:
  576. this.editorCtx.format(name, value)
  577. break
  578. }
  579. },
  580. // 悬浮工具点击
  581. fabTap(fabType) {
  582. if (this.curFab != fabType) {
  583. this.curFab = fabType
  584. } else {
  585. this.curFab = ''
  586. }
  587. },
  588. // 悬浮工具子集点击
  589. fabTapSub(e, fabType) {
  590. this.format(e)
  591. this.fabTap(fabType)
  592. },
  593. showPicker() {
  594. switch (this.curColor) {
  595. case 'color':
  596. this.defaultColor = this.textColor
  597. ? this.$refs.colorPickerRef.hex2Rgb(this.textColor)
  598. : { r: 0, g: 0, b: 0, a: 1 }
  599. break
  600. case 'backgroundColor':
  601. this.defaultColor = this.backgroundColor
  602. ? this.$refs.colorPickerRef.hex2Rgb(this.backgroundColor)
  603. : { r: 0, g: 0, b: 0, a: 0 }
  604. break
  605. }
  606. this.$refs.colorPickerRef.open()
  607. },
  608. confirmColor(e) {
  609. switch (this.curColor) {
  610. case 'color':
  611. this.textColor = e.hex
  612. this.editorCtx.format('color', this.textColor)
  613. break
  614. case 'backgroundColor':
  615. this.backgroundColor = e.hex
  616. this.editorCtx.format('backgroundColor', this.backgroundColor)
  617. break
  618. }
  619. },
  620. onStatusChange(e) {
  621. if (e.detail.color) {
  622. this.textColor = e.detail.color
  623. }
  624. if (e.detail.backgroundColor) {
  625. this.backgroundColor = e.detail.backgroundColor
  626. }
  627. this.formats = e.detail
  628. },
  629. insertDivider() {
  630. this.editorCtx.insertDivider()
  631. },
  632. clear() {
  633. uni.showModal({
  634. title: '清空编辑器',
  635. content: '确定清空编辑器吗?',
  636. success: ({ confirm }) => {
  637. if (confirm) {
  638. this.editorCtx.clear()
  639. }
  640. }
  641. })
  642. },
  643. removeFormat() {
  644. uni.showModal({
  645. title: '文本格式化',
  646. content: '确定要清除所选择部分文本块格式吗?',
  647. showCancel: true,
  648. success: ({ confirm }) => {
  649. if (confirm) {
  650. this.editorCtx.removeFormat()
  651. }
  652. }
  653. })
  654. },
  655. insertDate() {
  656. const date = new Date()
  657. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  658. this.editorCtx.insertText({ text: formatDate })
  659. },
  660. insertLink() {
  661. this.$refs.linkEditRef.open()
  662. },
  663. /**
  664. * 确认添加链接
  665. * @param {Object} e { text: '链接描述', href: '链接地址' }
  666. */
  667. confirmLink(e) {
  668. this.$refs.linkEditRef.close()
  669. addLink(this.editorCtx, e, () => {
  670. // 修复添加超链接后,不触发input更新当前最新内容的bug,这里做一下手动更新
  671. this.editorCtx.getContents({
  672. success: (res) => {
  673. this.$emit('input', { html: res.html, text: res.text }, this.editorId)
  674. }
  675. })
  676. })
  677. this.$emit('addLink', e, this.editorId)
  678. },
  679. insertImage() {
  680. // #ifdef APP-PLUS || H5
  681. uni.chooseImage({
  682. // count: 1, // 默认9
  683. success: (res) => {
  684. const { tempFiles } = res
  685. // 将文件和编辑器示例抛出,由开发者自行上传和插入图片
  686. this.$emit('upinImage', tempFiles, this.editorCtx, this.editorId)
  687. },
  688. fail() {
  689. uni.showToast({
  690. title: '未授权访问相册权限,请授权后使用',
  691. icon: 'none'
  692. })
  693. }
  694. })
  695. // #endif
  696. // #ifdef MP-WEIXIN
  697. // 微信小程序从基础库 2.21.0 开始, wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替。
  698. uni.chooseMedia({
  699. // count: 1, // 默认9
  700. mediaType: ['image'],
  701. success: (res) => {
  702. // 同上chooseImage处理
  703. const { tempFiles } = res
  704. this.$emit('upinImage', tempFiles, this.editorCtx, this.editorId)
  705. },
  706. fail() {
  707. uni.showToast({
  708. title: '未授权访问相册权限,请授权后使用',
  709. icon: 'none'
  710. })
  711. }
  712. })
  713. // #endif
  714. },
  715. insertVideo() {
  716. uni.chooseVideo({
  717. sourceType: ['camera', 'album'],
  718. success: (res) => {
  719. const { tempFilePath } = res
  720. // 将文件和编辑器示例抛出,由开发者自行上传和插入图片
  721. this.$emit('upinVideo', tempFilePath, this.editorCtx, this.editorId)
  722. },
  723. fail() {
  724. uni.showToast({
  725. title: '未授权访问媒体权限,请授权后使用',
  726. icon: 'none'
  727. })
  728. }
  729. })
  730. },
  731. onEditorInput(e) {
  732. // 注意不要使用getContents获取html和text,会导致重复触发onStatusChange从而失去toolbar工具的高亮状态
  733. // 复制粘贴的时候detail会为空,此时应当直接return
  734. if (Object.keys(e.detail).length <= 0) return
  735. const { html, text } = e.detail
  736. // 识别到标识立即return
  737. if (text.indexOf(linkFlag) !== -1) return
  738. const maxlength = parseInt(this.maxlength)
  739. const textStr = text.replace(/[ \t\r\n]/g, '')
  740. if (textStr.length > maxlength && maxlength != -1) {
  741. uni.showModal({
  742. content: `超过${maxlength}字数啦~`,
  743. confirmText: '确定',
  744. showCancel: false,
  745. success: () => {
  746. this.$emit('overMax', { html, text }, this.editorId)
  747. }
  748. })
  749. } else {
  750. this.$emit('input', { html, text }, this.editorId)
  751. }
  752. },
  753. // 导出
  754. exportHtml() {
  755. this.editorCtx.getContents({
  756. success: (res) => {
  757. this.$emit('exportHtml', res.html, this.editorId)
  758. }
  759. })
  760. },
  761. eLongpress() {
  762. /**
  763. * 微信小程序官方editor的长按事件有bug,需要重写覆盖,不需做任何逻辑,可见下面小程序社区问题链接
  764. * @tutorial https://developers.weixin.qq.com/community/develop/doc/000c04b3e1c1006f660065e4f61000
  765. */
  766. }
  767. }
  768. }
  769. </script>
  770. <style lang="scss">
  771. @import '@/uni_modules/sp-editor/icons/editor-icon.css';
  772. @import '@/uni_modules/sp-editor/icons/custom-icon.css';
  773. .sp-editor {
  774. height: 100%;
  775. display: flex;
  776. flex-direction: column;
  777. position: relative;
  778. }
  779. .sp-editor-toolbar {
  780. box-sizing: border-box;
  781. padding: calc(var(--icon-size) / 4) 0;
  782. border-bottom: 1px solid #e4e4e4;
  783. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  784. display: grid;
  785. grid-template-columns: repeat(var(--icon-columns), 1fr);
  786. }
  787. .iconfont {
  788. display: flex;
  789. align-items: center;
  790. justify-content: center;
  791. width: 100%;
  792. height: calc(var(--icon-size) * 1.8);
  793. cursor: pointer;
  794. font-size: var(--icon-size);
  795. }
  796. .sp-editor-wrapper {
  797. flex: 1;
  798. overflow: hidden;
  799. position: relative;
  800. }
  801. .editor-container {
  802. padding: 8rpx 16rpx;
  803. box-sizing: border-box;
  804. width: 100%;
  805. height: 100%;
  806. font-size: 16px;
  807. line-height: 1.5;
  808. }
  809. .ql-image-overlay-none {
  810. ::v-deep .ql-image-overlay {
  811. pointer-events: none;
  812. opacity: 0;
  813. }
  814. }
  815. ::v-deep .ql-editor.ql-blank::before {
  816. font-style: normal;
  817. color: #cccccc;
  818. }
  819. ::v-deep .ql-container {
  820. min-height: unset;
  821. }
  822. .ql-active {
  823. color: #66ccff;
  824. }
  825. .fab-tools {
  826. display: flex;
  827. padding: 0 10rpx;
  828. box-sizing: border-box;
  829. .fab-sub {
  830. width: auto;
  831. height: auto;
  832. margin: 10rpx;
  833. }
  834. }
  835. </style>