editor.uvue 9.6 KB
Newer Older
Anne_LXM's avatar
Anne_LXM 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3 4 5 6 7 8 9 10 11
  <view class="container">
    <view class="page-body">
      <view class='wrapper'>
        <view class='toolbar' @tap="format">
          <view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold"></view>
          <view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic"></view>
          <view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian"
            data-name="underline"></view>
          <view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian" data-name="strike">
          </view>
Anne_LXM's avatar
Anne_LXM 已提交
12

DCloud-WZF's avatar
DCloud-WZF 已提交
13 14
          <view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi" data-name="align"
            data-value="left"></view>
Anne_LXM's avatar
Anne_LXM 已提交
15

DCloud-WZF's avatar
DCloud-WZF 已提交
16 17 18 19 20 21
          <view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
            data-name="align" data-value="center"></view>
          <view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi" data-name="align"
            data-value="right"></view>
          <view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi"
            data-name="align" data-value="justify"></view>
Anne_LXM's avatar
Anne_LXM 已提交
22

DCloud-WZF's avatar
DCloud-WZF 已提交
23 24 25 26 27 28 29 30
          <view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height" data-name="lineHeight"
            data-value="2"></view>
          <view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-Character-Spacing"
            data-name="letterSpacing" data-value="2em"></view>
          <view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-722bianjiqi_duanqianju"
            data-name="marginTop" data-value="20px"></view>
          <view :class="formats.marginBottom ? 'ql-active' : ''" class="iconfont icon-723bianjiqi_duanhouju"
            data-name="marginBottom" data-value="20px"></view>
Anne_LXM's avatar
Anne_LXM 已提交
31

DCloud-WZF's avatar
DCloud-WZF 已提交
32 33 34 35 36 37 38 39
          <view :class="formats.fontFamily ? 'ql-active' : ''" class="iconfont icon-font" data-name="fontFamily"
            data-value="Pacifico"></view>
          <view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize"
            data-name="fontSize" data-value="24px"></view>
          <view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color"
            data-name="color" data-value="#0000ff"></view>
          <view :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''" class="iconfont icon-fontbgcolor"
            data-name="backgroundColor" data-value="#00ff00"></view>
Anne_LXM's avatar
Anne_LXM 已提交
40

DCloud-WZF's avatar
DCloud-WZF 已提交
41 42 43 44 45
          <view class="iconfont icon--checklist" data-name="list" data-value="check"></view>
          <view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie"
            data-name="list" data-value="ordered"></view>
          <view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie" data-name="list"
            data-value="bullet"></view>
Anne_LXM's avatar
Anne_LXM 已提交
46 47


DCloud-WZF's avatar
DCloud-WZF 已提交
48 49
          <view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
          <view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
Anne_LXM's avatar
Anne_LXM 已提交
50 51


DCloud-WZF's avatar
DCloud-WZF 已提交
52 53 54 55 56 57
          <view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
            data-name="header" :data-value="1"></view>
          <view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao"
            data-name="script" data-value="sub"></view>
          <view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao"
            data-name="script" data-value="super"></view>
Anne_LXM's avatar
Anne_LXM 已提交
58

DCloud-WZF's avatar
DCloud-WZF 已提交
59 60
          <view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl"
            data-name="direction" data-value="rtl"></view>
Anne_LXM's avatar
Anne_LXM 已提交
61 62 63 64 65 66 67 68

          <view class="iconfont icon-date" @tap="insertDate"></view>
          <view class="iconfont icon-fengexian" @tap="insertDivider"></view>
          <view class="iconfont icon-charutupian" @tap="chooseInsertImage"></view>
          <view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
          <view class="iconfont icon-undo" @tap="undo"></view>
          <view class="iconfont icon-redo" @tap="redo"></view>
          <view class="iconfont icon-shanchu" @tap="clearShowModal"></view>
DCloud-WZF's avatar
DCloud-WZF 已提交
69
        </view>
Anne_LXM's avatar
Anne_LXM 已提交
70

DCloud-WZF's avatar
DCloud-WZF 已提交
71 72 73 74 75
        <view class="editor-wrapper">
          <editor id="editor" class="ql-container" placeholder="开始输入..." show-img-size show-img-toolbar show-img-resize
            @statuschange="onStatusChange" :read-only="readOnly" @ready="onEditorReady">
          </editor>
        </view>
Anne_LXM's avatar
Anne_LXM 已提交
76 77 78
        <view>
          <button @tap="getCon">打印文本内容</button>
        </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
79 80 81
      </view>
    </view>
  </view>
Anne_LXM's avatar
Anne_LXM 已提交
82 83 84 85
</template>

<script lang="uts">
  type Context = {
DCloud-WZF's avatar
DCloud-WZF 已提交
86 87
    id ?: string;
    pageId ?: number;
Anne_LXM's avatar
Anne_LXM 已提交
88 89
  }

DCloud-WZF's avatar
DCloud-WZF 已提交
90 91 92 93 94 95
  export default {
    data() {
      return {
        readOnly: false,
        formats: {},
        editorCtx: {} as Context,
Anne_LXM's avatar
Anne_LXM 已提交
96
        // 自动化测试
DCloud-WZF's avatar
DCloud-WZF 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        autoTest: false,
        undoTest: false,
        redoTest: false,
        removeFormatTest: false,
        insertImageTest: false,
        blurTest: false
      }
    },
    onLoad() {
      uni.loadFontFace({
        family: 'Pacifico',
        source: 'url("/static/font/Pacifico-Regular.ttf")',
        success() {
          console.log('success load font')
        },
        fail() {
          console.log('fail load font load')
        }
      })
    },
    methods: {
      readOnlyChange() {
        this.readOnly = !this.readOnly
      },
      onEditorReady() {
        uni.createSelectorQuery().select('#editor').context((res) => {
          this.editorCtx = res.context
        }).exec()
      },
Anne_LXM's avatar
Anne_LXM 已提交
126 127
      // 自动化测试专用
      setContents(options) {
DCloud-WZF's avatar
DCloud-WZF 已提交
128
        this.editorCtx.setContents({
Anne_LXM's avatar
Anne_LXM 已提交
129
          delta: {
DCloud-WZF's avatar
DCloud-WZF 已提交
130
            ops: options
Anne_LXM's avatar
Anne_LXM 已提交
131 132
          },
          success: (res) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
133
            console.log('setContents-success', res)
Anne_LXM's avatar
Anne_LXM 已提交
134 135
          },
          fail: (err) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
136
            console.log(err)
Anne_LXM's avatar
Anne_LXM 已提交
137
          }
DCloud-WZF's avatar
DCloud-WZF 已提交
138
        })
Anne_LXM's avatar
Anne_LXM 已提交
139
      },
Anne_LXM's avatar
Anne_LXM 已提交
140
      blur() {
DCloud-WZF's avatar
DCloud-WZF 已提交
141 142 143
        this.editorCtx.blur({
          success: (res) => {
            console.log('编辑器失焦:', res)
Anne_LXM's avatar
Anne_LXM 已提交
144
            this.blurTest = true
DCloud-WZF's avatar
DCloud-WZF 已提交
145 146 147 148 149
          },
          fail: (err) => {
            console.log(err)
          }
        })
Anne_LXM's avatar
Anne_LXM 已提交
150
      },
Anne_LXM's avatar
Anne_LXM 已提交
151
      getCon() {
DCloud-WZF's avatar
DCloud-WZF 已提交
152 153 154 155 156 157 158 159
        this.editorCtx.getContents({
          success: (res) => {
            console.log('文本详情:', res)
          },
          fail: (err) => {
            console.log(err)
          }
        })
Anne_LXM's avatar
Anne_LXM 已提交
160
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
161 162 163
      undo() {
        this.editorCtx.undo({
          success: (res) => {
Anne_LXM's avatar
Anne_LXM 已提交
164
            this.undoTest = true
DCloud-WZF's avatar
DCloud-WZF 已提交
165 166
          },
          fail: (err) => {
Anne_LXM's avatar
Anne_LXM 已提交
167
            this.undoTest = false
DCloud-WZF's avatar
DCloud-WZF 已提交
168 169 170 171 172 173
          }
        })
      },
      redo() {
        this.editorCtx.redo({
          success: (res) => {
Anne_LXM's avatar
Anne_LXM 已提交
174
            this.redoTest = true
DCloud-WZF's avatar
DCloud-WZF 已提交
175 176
          },
          fail: (err) => {
Anne_LXM's avatar
Anne_LXM 已提交
177
            this.redoTest = false
DCloud-WZF's avatar
DCloud-WZF 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
          }
        })
      },
      format(e) {
        let { name, value } = e.target.dataset
        if (!name) return
        // console.log('format', name, value)
        this.editorCtx.format(name, value)
      },
      onStatusChange(e) {
        const formats = e.detail
        this.formats = formats
      },
      insertDivider() {
        this.editorCtx.insertDivider({
          success: function () {
            console.log('insert divider success')
          }
        })
      },
Anne_LXM's avatar
Anne_LXM 已提交
198 199
      clear() {
        this.editorCtx.clear({
DCloud-WZF's avatar
DCloud-WZF 已提交
200
          success: function (res) {
Anne_LXM's avatar
Anne_LXM 已提交
201 202 203 204
            console.log("clear success")
          }
        })
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
205
      clearShowModal() {
Anne_LXM's avatar
Anne_LXM 已提交
206
        uni.showModal({
DCloud-WZF's avatar
DCloud-WZF 已提交
207 208 209 210 211 212 213
          title: '清空编辑器',
          content: '确定清空编辑器全部内容?',
          success: res => {
            if (res.confirm) {
              this.clear()
            }
          }
Anne_LXM's avatar
Anne_LXM 已提交
214
        })
DCloud-WZF's avatar
DCloud-WZF 已提交
215 216 217 218 219
      },
      removeFormat() {
        this.editorCtx.removeFormat({
          success: (res) => {
            console.log('removeFormat-success', res)
Anne_LXM's avatar
Anne_LXM 已提交
220
            this.removeFormatTest = true
DCloud-WZF's avatar
DCloud-WZF 已提交
221 222
          },
          fail: (err) => {
Anne_LXM's avatar
Anne_LXM 已提交
223
            this.removeFormatTest = false
DCloud-WZF's avatar
DCloud-WZF 已提交
224 225 226 227 228 229 230 231 232 233 234
          }
        })
      },
      insertDate() {
        const date = new Date()
        const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
        this.editorCtx.insertText({
          text: formatDate
        })
      },
      insertImage(image) {
Anne_LXM's avatar
Anne_LXM 已提交
235
        this.editorCtx.insertImage({
DCloud-WZF's avatar
DCloud-WZF 已提交
236
          src: image,
Anne_LXM's avatar
Anne_LXM 已提交
237
          alt: '图像',
DCloud-WZF's avatar
DCloud-WZF 已提交
238
          success: () => {
Anne_LXM's avatar
Anne_LXM 已提交
239
            console.log('insert image success')
Anne_LXM's avatar
Anne_LXM 已提交
240
            this.insertImageTest = true
Anne_LXM's avatar
Anne_LXM 已提交
241 242 243
          }
        })
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
244
      chooseInsertImage() {
Anne_LXM's avatar
Anne_LXM 已提交
245
        uni.chooseImage({
DCloud-WZF's avatar
DCloud-WZF 已提交
246 247 248 249
          count: 1,
          success: (res) => {
            this.insertImage(res.tempFilePaths[0])
          }
Anne_LXM's avatar
Anne_LXM 已提交
250
        })
DCloud-WZF's avatar
DCloud-WZF 已提交
251 252 253
      }
    }
  }
Anne_LXM's avatar
Anne_LXM 已提交
254 255 256
</script>

<style>
DCloud-WZF's avatar
DCloud-WZF 已提交
257
  @import "./editor-icon.css";
Anne_LXM's avatar
Anne_LXM 已提交
258

DCloud-WZF's avatar
DCloud-WZF 已提交
259 260 261
  .page-body {
    height: calc(100vh - var(--window-top) - var(--status-bar-height));
  }
Anne_LXM's avatar
Anne_LXM 已提交
262

DCloud-WZF's avatar
DCloud-WZF 已提交
263 264 265
  .wrapper {
    height: 100%;
  }
Anne_LXM's avatar
Anne_LXM 已提交
266

DCloud-WZF's avatar
DCloud-WZF 已提交
267 268 269 270
  .editor-wrapper {
    height: calc(100vh - var(--window-top) - var(--status-bar-height) - 140px - 46px);
    background: #fff;
  }
Anne_LXM's avatar
Anne_LXM 已提交
271

DCloud-WZF's avatar
DCloud-WZF 已提交
272 273 274 275 276 277
  .iconfont {
    display: inline-block;
    width: 30px;
    height: 30px;
    cursor: pointer;
    font-size: 20px;
Anne_LXM's avatar
Anne_LXM 已提交
278 279
    margin: 0px 6px;
    align-content: center;
DCloud-WZF's avatar
DCloud-WZF 已提交
280
  }
Anne_LXM's avatar
Anne_LXM 已提交
281

DCloud-WZF's avatar
DCloud-WZF 已提交
282 283 284 285
  .toolbar {
    box-sizing: border-box;
    border-bottom: 0;
    font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
Anne_LXM's avatar
Anne_LXM 已提交
286 287
    flex-direction: row;
    flex-wrap: wrap;
Anne_LXM's avatar
Anne_LXM 已提交
288 289
    height: 140px;
    padding-left: 10px;
DCloud-WZF's avatar
DCloud-WZF 已提交
290
  }
Anne_LXM's avatar
Anne_LXM 已提交
291

DCloud-WZF's avatar
DCloud-WZF 已提交
292 293 294 295 296 297 298 299 300
  .ql-container {
    box-sizing: border-box;
    padding: 12px 15px;
    width: 100%;
    min-height: 30vh;
    height: 100%;
    font-size: 16px;
    line-height: 1.5;
  }
Anne_LXM's avatar
Anne_LXM 已提交
301

DCloud-WZF's avatar
DCloud-WZF 已提交
302 303 304
  .ql-active {
    color: #06c;
  }
Anne_LXM's avatar
Anne_LXM 已提交
305
</style>