robin-editor.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <view class="wrapper" :style="{ 'padding-top': keyboardHeight }">
  3. <!-- <robin-editor-header class="header" @cancel="cancel" @save="save" :labelConfirm="labelConfirm" :labelCancel="labelCancel"></robin-editor-header> -->
  4. <view class="toolbar" @tap="format" v-if="!showPreview" v-show="keyboardHeight || !autoHideToolbar" :style="'bottom:' + (isIOS ? keyboardHeight : 0) + 'px'">
  5. <block v-for="(t, i) in tools" :key="i">
  6. <view v-if="t == 'bold'" :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold" data-label="加粗"></view>
  7. <view v-if="t == 'italic'" :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic" data-label="斜体"></view>
  8. <view v-if="t == 'underline'" :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian" data-name="underline" data-label="下滑线"></view>
  9. <view v-if="t == 'strike'" :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian" data-name="strike" data-label="删除线"></view>
  10. <view
  11. v-if="t == 'align-left'"
  12. :class="formats.align === 'left' || !formats.align ? 'ql-active' : ''"
  13. class="iconfont icon-zuoduiqi"
  14. data-name="align"
  15. data-value="left"
  16. data-label="居左"
  17. ></view>
  18. <view
  19. v-if="t == 'align-center'"
  20. :class="formats.align === 'center' ? 'ql-active' : ''"
  21. class="iconfont icon-juzhongduiqi"
  22. data-name="align"
  23. data-value="center"
  24. data-label="居中"
  25. ></view>
  26. <view
  27. v-if="t == 'align-right'"
  28. :class="formats.align === 'right' ? 'ql-active' : ''"
  29. class="iconfont icon-youduiqi"
  30. data-name="align"
  31. data-value="right"
  32. data-label="居右"
  33. ></view>
  34. <view
  35. v-if="t == 'align-justify'"
  36. :class="formats.align === 'justify' ? 'ql-active' : ''"
  37. class="iconfont icon-zuoyouduiqi"
  38. data-name="align"
  39. data-value="justify"
  40. data-label="平铺"
  41. ></view>
  42. <!-- <view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height" data-name="lineHeight"
  43. data-value="2"></view>
  44. <view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-Character-Spacing" data-name="letterSpacing"
  45. data-value="2em"></view>
  46. <view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-722bianjiqi_duanqianju" data-name="marginTop"
  47. data-value="20px"></view>
  48. <view :class="formats.previewarginBottom ? 'ql-active' : ''" class="iconfont icon-723bianjiqi_duanhouju"
  49. data-name="marginBottom" data-value="20px"></view> -->
  50. <view v-if="t == 'remove'" class="iconfont icon-clearedformat" @tap.stop="removeFormat"></view>
  51. <picker v-if="t == 'font'" class="iconfont" mode="selector" :range="fontSizeRange" @change="fontSize"><view class="icon-fontsize"></view></picker>
  52. <view
  53. v-if="t == 'color'"
  54. :style="fontColor != '#FFFFFF' ? 'color:' + formats.color : ''"
  55. class="iconfont icon-text_color"
  56. data-name="color"
  57. @tap.stop="openColor"
  58. ></view>
  59. <view
  60. v-if="t == 'backgroundColor'"
  61. :style="bgColor ? 'color:' + formats.backgroundColor : ''"
  62. class="iconfont icon-fontbgcolor"
  63. data-name="backgroundColor"
  64. @tap.stop="openColor"
  65. ></view>
  66. <view v-if="t == 'image'" class="iconfont icon-charutupian" @tap.stop="insertImage"></view>
  67. <view v-if="t == 'clear'" class="iconfont icon-shanchu" @tap.stop="clear"></view>
  68. <view v-if="t == 'preview'" class="iconfont icon-preview" @tap.stop="preview"></view>
  69. <view v-if="t == 'date'" class="iconfont icon-date" @tap="insertDate"></view>
  70. <view v-if="t == 'list-check'" class="iconfont icon-checklist" data-name="list" data-value="check"></view>
  71. <view
  72. v-if="t == 'list-ordered'"
  73. :class="formats.list === 'ordered' ? 'ql-active' : ''"
  74. class="iconfont icon-youxupailie"
  75. data-name="list"
  76. data-value="ordered"
  77. ></view>
  78. <view v-if="t == 'list-bullet'" :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie" data-name="list" data-value="bullet"></view>
  79. <view v-if="t == 'undo'" class="iconfont icon-undo" @tap="undo"></view>
  80. <view v-if="t == 'redo'" class="iconfont icon-redo" @tap="redo"></view>
  81. <view v-if="t == 'outdent'" class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
  82. <view v-if="t == 'indent'" class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
  83. <view v-if="t == 'divider'" class="iconfont icon-fengexian" @tap="insertDivider"></view>
  84. <view v-if="t == 'h1'" :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1" data-name="header" :data-value="1"></view>
  85. <view v-if="t == 'h2'" :class="formats.header === 2 ? 'ql-active' : ''" class="iconfont icon-format-header-2" data-name="header" :data-value="2"></view>
  86. <view v-if="t == 'h3'" :class="formats.header === 3 ? 'ql-active' : ''" class="iconfont icon-format-header-3" data-name="header" :data-value="3"></view>
  87. <view v-if="t == 'h4'" :class="formats.header === 4 ? 'ql-active' : ''" class="iconfont icon-format-header-4" data-name="header" :data-value="4"></view>
  88. <view v-if="t == 'h5'" :class="formats.header === 5 ? 'ql-active' : ''" class="iconfont icon-format-header-5" data-name="header" :data-value="5"></view>
  89. <view v-if="t == 'h6'" :class="formats.header === 6 ? 'ql-active' : ''" class="iconfont icon-format-header-6" data-name="header" :data-value="6"></view>
  90. <view v-if="t == 'sub'" :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao" data-name="script" data-value="sub"></view>
  91. <view v-if="t == 'super'" :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao" data-name="script" data-value="super"></view>
  92. <view
  93. v-if="t == 'rtl'"
  94. :class="formats.direction === 'rtl' ? 'ql-active' : ''"
  95. class="iconfont icon-direction-rtl"
  96. data-name="direction"
  97. :data-value="formats.direction === 'rtl' ? '' : 'rtl'"
  98. ></view>
  99. </block>
  100. </view>
  101. <view :style="'height:' + editorHeight + 'px;'" class="container" v-if="!previewMode" v-show="!showPreview">
  102. <editor
  103. v-if="!previewMode"
  104. v-show="!showPreview"
  105. id="editor"
  106. class="ql-container"
  107. placeholder="开始输入..."
  108. showImgSize
  109. showImgToolbar
  110. showImgResize
  111. @statuschange="onStatusChange"
  112. :read-only="readOnly"
  113. @ready="onEditorReady"
  114. @blur="blur"
  115. ></editor>
  116. </view>
  117. <uni-popup type="bottom" ref="color"><robin-color-picker :color="color" @confirm="colorChanged"></robin-color-picker></uni-popup>
  118. <view class="preview" v-show="showPreview"><rich-text :nodes="htmlData" class="previewNodes"></rich-text></view>
  119. </view>
  120. </template>
  121. <script>
  122. export default {
  123. props: {
  124. value: {
  125. type: String
  126. },
  127. imageUploader: {
  128. type: Function
  129. },
  130. muiltImage: {
  131. type: Boolean,
  132. default: false
  133. },
  134. compressImage: {
  135. type: Boolean,
  136. default: false
  137. },
  138. previewMode: {
  139. type: Boolean,
  140. default: false
  141. },
  142. autoHideToolbar: {
  143. type: Boolean,
  144. default: false
  145. },
  146. tools: {
  147. type: Array,
  148. default: function() {
  149. return [
  150. 'bold',
  151. 'italic',
  152. 'underline',
  153. 'strike',
  154. 'align-left',
  155. 'align-center',
  156. 'align-right',
  157. 'remove',
  158. 'font',
  159. 'color',
  160. 'backgroundColor',
  161. 'image',
  162. 'clear',
  163. // 'preview'
  164. ];
  165. }
  166. }
  167. },
  168. data() {
  169. return {
  170. show: true,
  171. readOnly: false,
  172. formats: {},
  173. fontColor: '#000000',
  174. bgColor: '',
  175. color: '',
  176. colorPickerName: '',
  177. showColor: true,
  178. fontSizeRange: [10, 12, 14, 16, 18, 24, 32],
  179. showPreview: false,
  180. htmlData: '',
  181. html: '',
  182. keyboardHeight: 0,
  183. editorHeight: 0,
  184. isIOS: false
  185. };
  186. },
  187. watch: {
  188. value: function(newvar) {
  189. this.html = newvar;
  190. },
  191. html: function(newvar) {
  192. if (this.previewMode) {
  193. this.previewData(this.html);
  194. }
  195. if (this.editorCtx) {
  196. this.editorCtx.setContents({
  197. html: this.html
  198. });
  199. }
  200. }
  201. },
  202. created() {
  203. this.html = this.value;
  204. },
  205. mounted: function() {
  206. const platform = uni.getSystemInfoSync().platform;
  207. this.isIOS = platform === 'ios';
  208. if (this.previewMode) {
  209. this.previewData(this.html);
  210. }
  211. let keyboardHeight = 0;
  212. this.updatePosition(0);
  213. uni.onKeyboardHeightChange(res => {
  214. console.log(res, keyboardHeight);
  215. if (res.height === keyboardHeight) return;
  216. const duration = res.height > 0 ? res.duration * 1000 : 0;
  217. keyboardHeight = res.height;
  218. setTimeout(() => {
  219. uni.pageScrollTo({
  220. selector: "#editor",
  221. success: () => {
  222. this.updatePosition(keyboardHeight);
  223. this.editorCtx && this.editorCtx.scrollIntoView();
  224. }
  225. });
  226. }, duration);
  227. });
  228. },
  229. computed: {
  230. labelConfirm: function() {
  231. return this.showPreview ? '关闭' : '保存';
  232. },
  233. labelCancel: function() {
  234. return this.showPreview ? '' : '取消';
  235. }
  236. },
  237. methods: {
  238. updatePosition(keyboardHeight) {
  239. const { windowHeight, windowWidth, platform } = uni.getSystemInfoSync();
  240. const rpx = windowWidth / 750;
  241. let titleHeight = 0;
  242. //#ifdef H5
  243. titleHeight = 44; //H5标题栏高度
  244. //#endif
  245. const toolbarHeight = (70 * Math.ceil(this.tools.length / 15) + 1) * rpx; //底部工具栏高度
  246. const bodyHeight = windowHeight - titleHeight-300;
  247. this.keyboardHeight = keyboardHeight;
  248. this.editorHeight = keyboardHeight > 0 ? bodyHeight - keyboardHeight - toolbarHeight : this.autoHideToolbar ? bodyHeight : bodyHeight - toolbarHeight;
  249. },
  250. openColor(e) {
  251. var name = e.currentTarget.dataset.name;
  252. var color = this.formats[name];
  253. this.colorPickerName = name;
  254. if (name == 'backgroundColor' && !color) {
  255. color = '#FFFFFF';
  256. }
  257. if (name == 'color' && !color) {
  258. color = '#000000';
  259. }
  260. this.color = color;
  261. this.$refs.color.open(color);
  262. },
  263. colorChanged(e) {
  264. let label = '';
  265. switch (this.colorPickerName) {
  266. case 'backgroundColor':
  267. if (e.color == '#FFFFFF') {
  268. e.color = '';
  269. }
  270. this.bgColor = e.color;
  271. label = '背景色';
  272. break;
  273. case 'color':
  274. this.fontColor = e.color;
  275. label = '颜色';
  276. break;
  277. }
  278. this._format(this.colorPickerName, e.color, label + e.color);
  279. },
  280. readOnlyChange() {
  281. this.readOnly = !this.readOnly;
  282. },
  283. onEditorReady() {
  284. uni.createSelectorQuery()
  285. .in(this)
  286. .select('#editor')
  287. .context(res => {
  288. this.editorCtx = res.context;
  289. if (this.html) {
  290. this.editorCtx.setContents({
  291. html: this.html
  292. });
  293. }
  294. })
  295. .exec();
  296. },
  297. undo() {
  298. this.editorCtx.undo();
  299. this.toast('撤销');
  300. },
  301. redo() {
  302. this.editorCtx.redo();
  303. this.toast('重做');
  304. },
  305. format(e) {
  306. let { name, value, label } = e.target.dataset;
  307. if (!name) return;
  308. this._format(name, value, label);
  309. },
  310. _format(name, value, label) {
  311. this.editorCtx.format(name, value);
  312. this.toast(label);
  313. },
  314. toast(label) {
  315. uni.showToast({
  316. duration: 600,
  317. icon: 'none',
  318. title: label
  319. });
  320. },
  321. onStatusChange(e) {
  322. const formats = e.detail;
  323. this.formats = formats;
  324. },
  325. blur(e){
  326. this.save()
  327. },
  328. insertDivider() {
  329. this.editorCtx.insertDivider({
  330. success: function() {
  331. this.toast('插入分割线');
  332. }
  333. });
  334. },
  335. clear() {
  336. this.editorCtx.clear({
  337. success: res => {
  338. this.toast('清空');
  339. }
  340. });
  341. },
  342. removeFormat() {
  343. this.editorCtx.removeFormat();
  344. this.toast('清除格式');
  345. },
  346. insertDate() {
  347. const date = new Date();
  348. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  349. this.editorCtx.insertText({
  350. text: formatDate
  351. });
  352. this.toast('插入日期');
  353. },
  354. insertImage() {
  355. let params = {};
  356. params.count = this.muiltImage ? 9 : 1;
  357. params.sizeType = this.compressImage ? ['compressed'] : ['original'];
  358. uni.chooseImage({
  359. ...params,
  360. success: res => {
  361. res.tempFilePaths.map(path => {
  362. this.imageUploader(path, url => {
  363. this.editorCtx.insertImage({
  364. src: url,
  365. alt: '图像'
  366. });
  367. });
  368. });
  369. },
  370. fail:err=> {
  371. uni.showModal({
  372. content:JSON.stringify(err)
  373. })
  374. }
  375. });
  376. },
  377. fontSize(e) {
  378. const index = e.detail.value;
  379. const fz = this.fontSizeRange[index] + 'px';
  380. this._format('fontSize', fz, '字体大小:' + fz);
  381. },
  382. cancel() {
  383. this.$emit('cancel');
  384. },
  385. save() {
  386. if (this.showPreview) {
  387. if (this.previewMode) {
  388. this.cancel();
  389. } else {
  390. this.showPreview = false;
  391. }
  392. } else {
  393. this.editorCtx.getContents({
  394. success: res => {
  395. this.$emit('save', res);
  396. this.$emit('input', res.html);
  397. }
  398. });
  399. }
  400. },
  401. previewData: function(html) {
  402. this.htmlData = html.replace(/\<img/gi, '<img style="max-width:100%;height:auto"');
  403. this.showPreview = true;
  404. },
  405. preview: function() {
  406. this.editorCtx.getContents({
  407. success: res => {
  408. this.previewData(res.html);
  409. }
  410. });
  411. }
  412. }
  413. };
  414. </script>
  415. <style lang="scss" scoped>
  416. @import './editor-icon.css';
  417. .wrapper {
  418. padding: 5px;
  419. box-sizing: border-box;
  420. width: 100%;
  421. position: relative;
  422. border: 2upx solid #EEEEEE;
  423. .header {
  424. width: 100%;
  425. position: fixed;
  426. z-index: 9;
  427. left: 0;
  428. height: 75rpx;
  429. /* #ifndef H5 */
  430. top: 0;
  431. /* #endif */
  432. /* #ifdef H5 */
  433. top: 44px;
  434. /* #endif */
  435. }
  436. .container {
  437. width: 100%;
  438. // margin-top: 75rpx;
  439. background: #fff;
  440. .ql-container {
  441. box-sizing: border-box;
  442. width: 100%;
  443. height: 100%;
  444. font-size: 16px;
  445. line-height: 1.5;
  446. overflow: auto;
  447. padding: 20rpx;
  448. }
  449. }
  450. .toolbar {
  451. // position: fixed;
  452. // width: 100%;
  453. // left: 0;
  454. // bottom: 0;
  455. box-sizing: border-box;
  456. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  457. background-color: #fff;
  458. border-bottom: 2upx solid #eee;
  459. line-height: 50rpx;
  460. .iconfont {
  461. display: inline-block;
  462. padding: 10rpx;
  463. margin: 0 10upx;
  464. width: 50rpx;
  465. text-align: center;
  466. font-size: 42rpx;
  467. box-sizing: border-box;
  468. }
  469. }
  470. }
  471. .preview {
  472. width: 100%;
  473. margin-top: 90rpx;
  474. .previewNodes {
  475. width: 100%;
  476. word-break: break-all;
  477. }
  478. }
  479. .ql-active {
  480. color: #06c;
  481. }
  482. </style>