editor.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <template>
  2. <view class="container">
  3. <view class="page-body">
  4. <view class="wrapper">
  5. <view class="toolbar" @tap="format">
  6. <view
  7. :class="formats.bold ? 'ql-active' : ''"
  8. class="iconfont icon-zitijiacu"
  9. data-name="bold"
  10. >
  11. </view>
  12. <view
  13. :class="formats.italic ? 'ql-active' : ''"
  14. class="iconfont icon-zitixieti"
  15. data-name="italic"
  16. >
  17. </view>
  18. <view class="iconfont icon-charutupian" @tap="insertImage"></view>
  19. <view
  20. :class="formats.underline ? 'ql-active' : ''"
  21. class="iconfont icon-zitixiahuaxian"
  22. data-name="underline"
  23. ></view>
  24. <view
  25. :class="formats.strike ? 'ql-active' : ''"
  26. class="iconfont icon-zitishanchuxian"
  27. data-name="strike"
  28. ></view>
  29. <view
  30. :class="formats.align === 'left' ? 'ql-active' : ''"
  31. class="iconfont icon-zuoduiqi"
  32. data-name="align"
  33. data-value="left"
  34. ></view>
  35. <view
  36. :class="formats.align === 'center' ? 'ql-active' : ''"
  37. class="iconfont icon-juzhongduiqi"
  38. data-name="align"
  39. data-value="center"
  40. ></view>
  41. <view
  42. :class="formats.align === 'right' ? 'ql-active' : ''"
  43. class="iconfont icon-youduiqi"
  44. data-name="align"
  45. data-value="right"
  46. ></view>
  47. <view
  48. :class="formats.align === 'justify' ? 'ql-active' : ''"
  49. class="iconfont icon-zuoyouduiqi"
  50. data-name="align"
  51. data-value="justify"
  52. ></view>
  53. <view
  54. :class="formats.lineHeight ? 'ql-active' : ''"
  55. class="iconfont icon-line-height"
  56. data-name="lineHeight"
  57. data-value="2"
  58. ></view>
  59. <view
  60. :class="formats.letterSpacing ? 'ql-active' : ''"
  61. class="iconfont icon-Character-Spacing"
  62. data-name="letterSpacing"
  63. data-value="2em"
  64. ></view>
  65. <view
  66. :class="formats.marginTop ? 'ql-active' : ''"
  67. class="iconfont icon-722bianjiqi_duanqianju"
  68. data-name="marginTop"
  69. data-value="20px"
  70. ></view>
  71. <view
  72. :class="formats.previewarginBottom ? 'ql-active' : ''"
  73. class="iconfont icon-723bianjiqi_duanhouju"
  74. data-name="marginBottom"
  75. data-value="20px"
  76. ></view>
  77. <view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
  78. <view
  79. :class="formats.fontFamily ? 'ql-active' : ''"
  80. class="iconfont icon-font"
  81. data-name="fontFamily"
  82. data-value="Pacifico"
  83. ></view>
  84. <view
  85. :class="formats.fontSize === '24px' ? 'ql-active' : ''"
  86. class="iconfont icon-fontsize"
  87. data-name="fontSize"
  88. data-value="24px"
  89. ></view>
  90. <view
  91. :class="formats.color === '#0000ff' ? 'ql-active' : ''"
  92. class="iconfont icon-text_color"
  93. data-name="color"
  94. data-value="#0000ff"
  95. ></view>
  96. <view
  97. :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''"
  98. class="iconfont icon-fontbgcolor"
  99. data-name="backgroundColor"
  100. data-value="#00ff00"
  101. ></view>
  102. <view class="iconfont icon-date" @tap="insertDate"></view>
  103. <view
  104. class="iconfont icon--checklist"
  105. data-name="list"
  106. data-value="check"
  107. ></view>
  108. <view
  109. :class="formats.list === 'ordered' ? 'ql-active' : ''"
  110. class="iconfont icon-youxupailie"
  111. data-name="list"
  112. data-value="ordered"
  113. ></view>
  114. <view
  115. :class="formats.list === 'bullet' ? 'ql-active' : ''"
  116. class="iconfont icon-wuxupailie"
  117. data-name="list"
  118. data-value="bullet"
  119. ></view>
  120. <view class="iconfont icon-undo" @tap="undo"></view>
  121. <view class="iconfont icon-redo" @tap="redo"></view>
  122. <view
  123. class="iconfont icon-outdent"
  124. data-name="indent"
  125. data-value="-1"
  126. ></view>
  127. <view
  128. class="iconfont icon-indent"
  129. data-name="indent"
  130. data-value="+1"
  131. ></view>
  132. <view class="iconfont icon-fengexian" @tap="insertDivider"></view>
  133. <view
  134. :class="formats.header === 1 ? 'ql-active' : ''"
  135. class="iconfont icon-format-header-1"
  136. data-name="header"
  137. :data-value="1"
  138. ></view>
  139. <view
  140. :class="formats.script === 'sub' ? 'ql-active' : ''"
  141. class="iconfont icon-zitixiabiao"
  142. data-name="script"
  143. data-value="sub"
  144. ></view>
  145. <view
  146. :class="formats.script === 'super' ? 'ql-active' : ''"
  147. class="iconfont icon-zitishangbiao"
  148. data-name="script"
  149. data-value="super"
  150. ></view>
  151. <view class="iconfont icon-shanchu" @tap="clear"></view>
  152. <view
  153. :class="formats.direction === 'rtl' ? 'ql-active' : ''"
  154. class="iconfont icon-direction-rtl"
  155. data-name="direction"
  156. data-value="rtl"
  157. ></view>
  158. </view>
  159. <view class="editor-wrapper">
  160. <editor
  161. id="editor"
  162. class="ql-container"
  163. :placeholder="placeholder"
  164. @statuschange="onStatusChange"
  165. :show-img-resize="true"
  166. @ready="onEditorReady"
  167. @input="getCtx"
  168. ></editor>
  169. </view>
  170. </view>
  171. </view>
  172. </view>
  173. </template>
  174. <script>
  175. import { baseURL, phpToken } from "@/commit/requestPhp.js";
  176. export default {
  177. props: {
  178. value: {
  179. type: String,
  180. default: "",
  181. },
  182. placeholder: {
  183. type: String,
  184. default: "请输入商品详情信息...",
  185. },
  186. readOnly: {
  187. type: Boolean,
  188. default: false,
  189. },
  190. },
  191. data() {
  192. return {
  193. richText: "",
  194. formats: {},
  195. editorCtx: null,
  196. // 新增:记录光标位置
  197. cursorPosition: 0,
  198. // 新增:记录当前选中的文本
  199. selectedText: "",
  200. };
  201. },
  202. watch: {
  203. value: {
  204. handler(val,old) {
  205. this.richText = val;
  206. console.log(val)
  207. // this.setHTMLContent();
  208. },
  209. },
  210. },
  211. mounted() {
  212. this.richText = this.value;
  213. if (this.richText) {
  214. this.setHTMLContent();
  215. }
  216. },
  217. methods: {
  218. setHTMLContent(data) {
  219. if (this.editorCtx) {
  220. this.editorCtx.setContents({
  221. html:data|| this.richText,
  222. });
  223. }
  224. },
  225. readOnlyChange() {
  226. this.readOnly = !this.readOnly;
  227. },
  228. async onEditorReady() {
  229. // 富文本节点渲染完成
  230. await this.$nextTick();
  231. const query = uni.createSelectorQuery().in(this);
  232. query
  233. .select("#editor")
  234. .context((res) => {
  235. this.editorCtx = res.context;
  236. console.log(this.richText, "this.richTextthis.richText");
  237. if (this.richText) {
  238. this.setHTMLContent();
  239. }
  240. })
  241. .exec();
  242. },
  243. // 失去焦点时,获取富文本的内容
  244. getCtx(e) {
  245. this.richText = e.detail.html;
  246. this.$emit("input", e.detail.html);
  247. // 记录光标位置
  248. // this.cursorPosition = this.editorCtx.getSelection().index;
  249. // 记录当前选中的文本
  250. // this.selectedText = this.editorCtx.getSelectedText();
  251. },
  252. undo() {
  253. this.editorCtx.undo();
  254. },
  255. redo() {
  256. this.editorCtx.redo();
  257. },
  258. format(e) {
  259. let { name, value } = e.target.dataset;
  260. if (!name) return;
  261. // console.log('format', name, value)
  262. this.editorCtx.format(name, value);
  263. },
  264. onStatusChange(e) {
  265. console.log(e, "onStatusChange");
  266. e.preventDefault();
  267. e.stopPropagation();
  268. const formats = e.detail;
  269. this.formats = formats;
  270. },
  271. insertDivider() {
  272. this.editorCtx.insertDivider({
  273. success: function () {
  274. console.log("insert divider success");
  275. },
  276. });
  277. },
  278. clear() {
  279. this.editorCtx.clear({
  280. success: function (res) {
  281. console.log("clear success");
  282. },
  283. });
  284. },
  285. removeFormat() {
  286. this.editorCtx.removeFormat();
  287. },
  288. insertDate() {
  289. const date = new Date();
  290. const formatDate = `${date.getFullYear()}/${
  291. date.getMonth() + 1
  292. }/${date.getDate()}`;
  293. this.editorCtx.insertText({
  294. text: formatDate,
  295. });
  296. },
  297. insertImage() {
  298. var that = this;
  299. uni.chooseImage({
  300. count: 1, // 最多可以选择的图片张数,默认9
  301. // sizeType: ["compressed"], // original 原图,compressed 压缩图,默认二者都有
  302. sourceType: ["album", "camera"], // album 从相册选图,camera 使用相机,默认二者都有
  303. async success(res) {
  304. console.log("选择图片成功");
  305. console.log(res);
  306. const tempFiles = res.tempFiles;
  307. const data = await that.uploadFileData(tempFiles[0]);
  308. that.editorCtx.insertImage({
  309. width: "50%",
  310. height: "20%",
  311. src: data,
  312. });
  313. },
  314. });
  315. },
  316. async uploadFileData(files) {
  317. uni.showLoading({
  318. title: "上传中",
  319. mask: true,
  320. });
  321. const uploadUrl = `${baseURL}/mer/upload/image/0/file`;
  322. return new Promise((resolve, reject) => {
  323. uni.uploadFile({
  324. url: uploadUrl,
  325. file: files,
  326. name: "file",
  327. header: {
  328. "X-Token": phpToken,
  329. // 'Content-Type': "application/json;charset=UTF-8",
  330. },
  331. success: (res) => {
  332. const data = JSON.parse(res.data);
  333. if (data.status === 200) {
  334. uni.showToast({
  335. title: "上传成功",
  336. icon: "success",
  337. });
  338. this.paths = data.data.src || "";
  339. } else {
  340. uni.showToast({
  341. title: "上传失败",
  342. icon: "error",
  343. });
  344. this.paths = "";
  345. }
  346. resolve(this.paths);
  347. console.log("上传成功", JSON.parse(res.data));
  348. uni.hideLoading();
  349. uni.hideToast();
  350. },
  351. fail: (err) => {
  352. console.error("上传失败", err);
  353. this.paths = "";
  354. resolve(this.paths);
  355. uni.hideLoading();
  356. uni.hideToast();
  357. },
  358. complete: () => {
  359. uni.hideLoading();
  360. uni.hideToast();
  361. },
  362. });
  363. });
  364. },
  365. },
  366. };
  367. </script>
  368. <style>
  369. @import "./editor-icon.css";
  370. .page-body {
  371. height: calc(100% - var(--window-top) - var(--status-bar-height));
  372. }
  373. .wrapper {
  374. height: 100%;
  375. }
  376. .editor-wrapper {
  377. height: calc(100vh - var(--window-top) - var(--status-bar-height) - 140px);
  378. background: #eee;
  379. }
  380. .iconfont {
  381. display: inline-block;
  382. padding: 16rpx;
  383. width: 34rpx;
  384. height: 34rpx;
  385. cursor: pointer;
  386. font-size: 40rpx;
  387. }
  388. .toolbar {
  389. box-sizing: border-box;
  390. border-bottom: 0;
  391. font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif;
  392. }
  393. .ql-container {
  394. box-sizing: border-box;
  395. padding: 24rpx 30rpx;
  396. width: 100%;
  397. min-height: 30vh;
  398. height: 100%;
  399. margin-top: 20rpx;
  400. font-size: 32rpx;
  401. line-height: 1.5;
  402. }
  403. .ql-active {
  404. color: #06c;
  405. }
  406. </style>