editor.uvue 9.0 KB
Newer Older
Anne_LXM's avatar
Anne_LXM 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<template>
	<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>

					<view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi"
						data-name="align" data-value="left"></view>

					<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>

					<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 已提交
33

Anne_LXM's avatar
Anne_LXM 已提交
34 35 36 37 38 39 40 41
					<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 已提交
42

Anne_LXM's avatar
Anne_LXM 已提交
43 44 45 46 47
					<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 已提交
48 49


Anne_LXM's avatar
Anne_LXM 已提交
50 51
					<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 已提交
52 53


Anne_LXM's avatar
Anne_LXM 已提交
54 55 56 57 58 59
					<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 已提交
60

Anne_LXM's avatar
Anne_LXM 已提交
61 62
					<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 已提交
63 64 65 66 67 68 69 70

          <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>
Anne_LXM's avatar
Anne_LXM 已提交
71 72 73 74 75 76 77
				</view>

				<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 已提交
78 79 80
        <view>
          <button @tap="getCon">打印文本内容</button>
        </view>
Anne_LXM's avatar
Anne_LXM 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
			</view>
		</view>
	</view>
</template>

<script lang="uts">
  type Context = {
  	id?: string;
  	pageId?: number;
  }

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

<style>
	@import "./editor-icon.css";

	.page-body {
		height: calc(100vh - var(--window-top) - var(--status-bar-height));
	}

	.wrapper {
		height: 100%;
	}

	.editor-wrapper {
Anne_LXM's avatar
Anne_LXM 已提交
270
		height: calc(100vh - var(--window-top) - var(--status-bar-height) - 140px - 46px);
Anne_LXM's avatar
Anne_LXM 已提交
271 272 273 274 275 276 277 278 279
		background: #fff;
	}

	.iconfont {
		display: inline-block;
		width: 30px;
		height: 30px;
		cursor: pointer;
		font-size: 20px;
Anne_LXM's avatar
Anne_LXM 已提交
280 281
    margin: 0px 6px;
    align-content: center;
Anne_LXM's avatar
Anne_LXM 已提交
282 283 284 285 286 287 288 289
	}

	.toolbar {
		box-sizing: border-box;
		border-bottom: 0;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
    flex-direction: row;
    flex-wrap: wrap;
Anne_LXM's avatar
Anne_LXM 已提交
290 291
    height: 140px;
    padding-left: 10px;
Anne_LXM's avatar
Anne_LXM 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	}

	.ql-container {
		box-sizing: border-box;
		padding: 12px 15px;
		width: 100%;
		min-height: 30vh;
		height: 100%;
		font-size: 16px;
		line-height: 1.5;
	}

	.ql-active {
		color: #06c;
	}
</style>