refactor: 确立独立子工程架构 (backend, admin, mpuniapp) 并同步更新 AGENTS.md 规范
这个提交包含在:
@@ -0,0 +1,414 @@
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isMP = platformInfo.startsWith('mp')
|
||||
const isIos = platformInfo.startsWith('ios')
|
||||
const isDom2 = process.env.UNI_APP_X_DOM2 === 'true'
|
||||
const isHarmony = platformInfo.startsWith('harmony')
|
||||
const isWeb = platformInfo.startsWith('web')
|
||||
const isAndroid = platformInfo.startsWith('android')
|
||||
|
||||
describe('editor-edit.uvue', () => {
|
||||
const infos = process.env.uniTestPlatformInfo.split(' ');
|
||||
const version = parseInt(infos[infos.length - 1]);
|
||||
/**
|
||||
* mp-weixin 自动化测试截图报错
|
||||
*
|
||||
* 暂时跳过相关平台的测试,后续根据平台能力完善测试用例
|
||||
*/
|
||||
if (isMP || (isDom2 && isIos) || (isAndroid && !isNaN(version) && version < 8)) {
|
||||
it('skip', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
let page
|
||||
let editor
|
||||
|
||||
const defaultFormats = {
|
||||
bold: false,
|
||||
italic: false,
|
||||
underline: false,
|
||||
strike: false,
|
||||
blockquote: false,
|
||||
codeBlock: false,
|
||||
link: '',
|
||||
header: 0,
|
||||
list: '',
|
||||
align: '',
|
||||
textIndent: '',
|
||||
marginLeft: '',
|
||||
marginRight: '',
|
||||
lineHeight: '',
|
||||
letterSpacing: '',
|
||||
fontFamily: '',
|
||||
fontSize: '',
|
||||
color: '',
|
||||
backgroundColor: ''
|
||||
}
|
||||
|
||||
async function loadPage() {
|
||||
page = await program.reLaunch('/pages/component/editor/editor-edit')
|
||||
await page.waitFor('view')
|
||||
await waitForData('data.editorReadyTest', value => value === true, isWeb ? 4000 : 8000)
|
||||
editor = await page.$('#editor')
|
||||
await updateData({
|
||||
autoTest: true
|
||||
})
|
||||
}
|
||||
|
||||
async function resetPageState() {
|
||||
await page.callMethod('closeSheets')
|
||||
await page.callMethod('resetFormatsForTest')
|
||||
await updateData({
|
||||
autoTest: true,
|
||||
undoTest: false,
|
||||
redoTest: false,
|
||||
removeFormatTest: false,
|
||||
insertImageTest: false,
|
||||
blurTest: false,
|
||||
clearTest: false,
|
||||
getContentDeltaTest: null,
|
||||
activeSheet: ''
|
||||
})
|
||||
await setEditorContents([
|
||||
{ insert: '\n' }
|
||||
])
|
||||
}
|
||||
|
||||
async function waitForData(path, matcher, timeout = 3000) {
|
||||
const start = Date.now()
|
||||
await page.waitFor(async () => {
|
||||
const value = await page.data(path)
|
||||
return matcher(value) || (Date.now() - start > timeout)
|
||||
})
|
||||
}
|
||||
|
||||
async function updateData(partial) {
|
||||
await page.setData({
|
||||
data: partial
|
||||
})
|
||||
}
|
||||
|
||||
async function applyToolbarPresetState(preset) {
|
||||
await page.callMethod('applyToolbarPresetForTest', preset)
|
||||
}
|
||||
|
||||
async function openSheet(methodName, sheetName, titleText, subtitleText) {
|
||||
await page.callMethod(methodName)
|
||||
await waitForData('data.activeSheet', value => value === sheetName, 2000)
|
||||
let currentTitle = ''
|
||||
let currentSubtitle = ''
|
||||
for (let i = 0; i < 10; i++) {
|
||||
await page.waitFor(100)
|
||||
currentTitle = await page.callMethod('getPanelTitle')
|
||||
currentSubtitle = await page.callMethod('getPanelSubtitle')
|
||||
if (currentTitle === titleText && currentSubtitle === subtitleText) {
|
||||
break
|
||||
}
|
||||
}
|
||||
expect(currentTitle).toBe(titleText)
|
||||
expect(currentSubtitle).toBe(subtitleText)
|
||||
}
|
||||
|
||||
async function closeSheet() {
|
||||
await page.callMethod('closeSheets')
|
||||
await waitForData('data.activeSheet', value => value === '', 2000)
|
||||
}
|
||||
|
||||
async function waitForFlag(path, timeout = 3000) {
|
||||
await waitForData(path, value => value === true, timeout)
|
||||
expect(await page.data(path)).toBe(true)
|
||||
}
|
||||
|
||||
async function waitForFormats(partial, timeout = 3000) {
|
||||
const keys = Object.keys(partial)
|
||||
await waitForData('data.formats', value => {
|
||||
return keys.every(key => value != null && value[key] === partial[key])
|
||||
}, timeout)
|
||||
expect(await page.data('data.formats')).toMatchObject(partial)
|
||||
}
|
||||
|
||||
async function waitForChecklistFormat(timeout = 3000) {
|
||||
await waitForData('data.formats', value => {
|
||||
return value != null && (value.list === 'check' || value.list === 'unchecked')
|
||||
}, timeout)
|
||||
const formats = await page.data('data.formats')
|
||||
expect(['check', 'unchecked']).toContain(formats.list)
|
||||
}
|
||||
|
||||
async function setBlur() {
|
||||
await updateData({
|
||||
blurTest: false
|
||||
})
|
||||
await page.callMethod('blur')
|
||||
await waitForFlag('data.blurTest', 2000)
|
||||
}
|
||||
|
||||
async function setEditorContents(ops) {
|
||||
await page.callMethod('setContents', ops)
|
||||
await page.waitFor(500)
|
||||
}
|
||||
|
||||
async function getDelta() {
|
||||
await updateData({
|
||||
getContentDeltaTest: null
|
||||
})
|
||||
await page.callMethod('getCon')
|
||||
await waitForData('data.getContentDeltaTest', value => value != null, 3000)
|
||||
return await page.data('data.getContentDeltaTest')
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
await loadPage()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetPageState()
|
||||
})
|
||||
|
||||
it('editor-wrapper', async () => {
|
||||
expect(await page.data('data.activeSheet')).toBe('')
|
||||
expect(await page.data('data.formats')).toEqual(defaultFormats)
|
||||
if (isWeb) {
|
||||
expect(await editor.attribute('placeholder')).toBe('请输入正文内容...')
|
||||
}
|
||||
expect(await program.screenshot({
|
||||
fullPage: true
|
||||
})).toSaveImageSnapshot()
|
||||
})
|
||||
|
||||
it('editor-toolbar', async () => {
|
||||
await openSheet('openMoreSheet', 'more', '更多操作', '插入与编辑快捷操作')
|
||||
await openSheet('openTitleSheet', 'title', '设置标题', '当前为正文')
|
||||
await openSheet('openStyleSheet', 'style', '设置字格式', '当前未设置字格式')
|
||||
await openSheet('openTextColorSheet', 'text-color', '设置文字颜色', '当前使用默认文字颜色')
|
||||
await openSheet('openBackgroundColorSheet', 'background-color', '设置背景颜色', '当前未设置文字背景颜色')
|
||||
await openSheet('openLineHeightSheet', 'line-height', '设置行间距', '当前使用默认行间距')
|
||||
await openSheet('openLetterSpacingSheet', 'letter-spacing', '设置字间距', '当前使用默认字间距')
|
||||
await openSheet('openFontSizeSheet', 'font-size', '设置字号', '当前使用默认字号 17px')
|
||||
await openSheet('openFontFamilySheet', 'font-family', '设置字体', '当前使用默认字体')
|
||||
await openSheet('openAlignSheet', 'align', '对齐方式', '当前为默认对齐')
|
||||
await openSheet('openBlockIndentSheet', 'block-indent', '设置两端缩进', '当前未设置两端缩进')
|
||||
await openSheet('openListSheet', 'list', '设置列表', '当前未设置列表')
|
||||
await closeSheet()
|
||||
})
|
||||
|
||||
it('editor-screenshot', async () => {
|
||||
// await openSheet('openStyleSheet', 'style', '设置字格式', '当前未设置字格式')
|
||||
expect(await program.screenshot({
|
||||
fullPage: true
|
||||
})).toSaveImageSnapshot()
|
||||
// await closeSheet()
|
||||
})
|
||||
|
||||
it('title-toolbar-actions', async () => {
|
||||
await applyToolbarPresetState('title-h2')
|
||||
await waitForFormats({
|
||||
header: 2,
|
||||
list: ''
|
||||
})
|
||||
expect(await page.callMethod('getTitleSummary')).toBe('当前为大标题2')
|
||||
await applyToolbarPresetState('title-h1')
|
||||
await waitForFormats({
|
||||
header: 1,
|
||||
list: ''
|
||||
})
|
||||
expect(await page.callMethod('getTitleSummary')).toBe('当前为大标题1')
|
||||
await page.callMethod('toggleCodeBlock')
|
||||
await waitForFormats({
|
||||
codeBlock: true,
|
||||
header: 0,
|
||||
list: ''
|
||||
})
|
||||
expect(await page.callMethod('getTitleSummary')).toBe('当前为代码块')
|
||||
})
|
||||
|
||||
it('code-block-toolbar-actions', async () => {
|
||||
await page.callMethod('toggleCodeBlock')
|
||||
await waitForFormats({
|
||||
codeBlock: true,
|
||||
header: 0,
|
||||
list: ''
|
||||
})
|
||||
expect(await page.callMethod('getTitleSummary')).toBe('当前为代码块')
|
||||
await page.callMethod('setHeadingLevel', 2)
|
||||
await waitForFormats({
|
||||
codeBlock: false,
|
||||
header: 2
|
||||
})
|
||||
expect(await page.callMethod('getTitleSummary')).toBe('当前为大标题2')
|
||||
await page.callMethod('toggleCodeBlock')
|
||||
await waitForFormats({
|
||||
codeBlock: true,
|
||||
header: 0
|
||||
})
|
||||
await page.callMethod('toggleCodeBlock')
|
||||
await waitForFormats({
|
||||
codeBlock: false,
|
||||
header: 0,
|
||||
list: ''
|
||||
})
|
||||
})
|
||||
|
||||
it('style-toolbar-actions', async () => {
|
||||
await page.callMethod('toggleBold')
|
||||
await page.callMethod('toggleItalic')
|
||||
await page.callMethod('toggleUnderline')
|
||||
await page.callMethod('toggleStrike')
|
||||
await waitForFormats({
|
||||
bold: true,
|
||||
italic: true,
|
||||
underline: true,
|
||||
strike: true
|
||||
})
|
||||
await openSheet('openStyleSheet', 'style', '设置字格式', '加粗 / 斜体 / 下划线 / 删除线')
|
||||
await page.callMethod('toggleBold')
|
||||
await page.callMethod('toggleItalic')
|
||||
await page.callMethod('toggleUnderline')
|
||||
await page.callMethod('toggleStrike')
|
||||
await waitForFormats({
|
||||
bold: false,
|
||||
italic: false,
|
||||
underline: false,
|
||||
strike: false
|
||||
})
|
||||
await closeSheet()
|
||||
})
|
||||
|
||||
it('extended-style-toolbar-actions', async () => {
|
||||
await page.callMethod('setTextColorAndClose', '#3553ff')
|
||||
await waitForFormats({ color: '#3553ff' })
|
||||
await openSheet('openTextColorSheet', 'text-color', '设置文字颜色', '当前文字颜色 #3553ff')
|
||||
await page.callMethod('setBackgroundColorAndClose', '#fff7db')
|
||||
await waitForFormats({ backgroundColor: '#fff7db' })
|
||||
await openSheet('openBackgroundColorSheet', 'background-color', '设置背景颜色', '当前背景颜色 #fff7db')
|
||||
await page.callMethod('setLineHeightAndClose', '1.8')
|
||||
await page.callMethod('setLetterSpacingAndClose', '2px')
|
||||
await page.callMethod('setFontSizeAndClose', '24px')
|
||||
await page.callMethod('setFontFamilyAndClose', 'Georgia')
|
||||
await waitForFormats({
|
||||
lineHeight: '1.8',
|
||||
letterSpacing: '2px',
|
||||
fontSize: '24px',
|
||||
fontFamily: 'Georgia'
|
||||
})
|
||||
await openSheet('openStyleSheet', 'style', '设置字格式', '行间距 1.8 / 字间距 2px / 字号 24px / 字体 Geo')
|
||||
await closeSheet()
|
||||
})
|
||||
|
||||
it('layout-toolbar-actions', async () => {
|
||||
await page.callMethod('setAlignCenter')
|
||||
await waitForFormats({ align: 'center' })
|
||||
await openSheet('openAlignSheet', 'align', '对齐方式', '当前为居中')
|
||||
await page.callMethod('toggleTextIndent')
|
||||
await waitForFormats({ textIndent: '2em' })
|
||||
await page.callMethod('setBlockIndentAndClose', '16px')
|
||||
await waitForFormats({
|
||||
marginLeft: '16px',
|
||||
marginRight: '16px'
|
||||
})
|
||||
await openSheet('openBlockIndentSheet', 'block-indent', '设置两端缩进', '当前两端缩进 16px')
|
||||
await closeSheet()
|
||||
})
|
||||
|
||||
it('list-toolbar-actions', async () => {
|
||||
await applyToolbarPresetState('list-bullet')
|
||||
await waitForFormats({
|
||||
header: 0,
|
||||
list: 'bullet'
|
||||
})
|
||||
await openSheet('openListSheet', 'list', '设置列表', '当前列表 无序列表')
|
||||
await closeSheet()
|
||||
await applyToolbarPresetState('list-ordered')
|
||||
await waitForFormats({
|
||||
header: 0,
|
||||
list: 'ordered'
|
||||
})
|
||||
await openSheet('openListSheet', 'list', '设置列表', '当前列表 有序列表')
|
||||
await closeSheet()
|
||||
await applyToolbarPresetState('list-unchecked')
|
||||
await waitForChecklistFormat()
|
||||
await applyToolbarPresetState('list-none')
|
||||
await waitForFormats({ list: '' })
|
||||
await closeSheet()
|
||||
})
|
||||
|
||||
it('clear', async () => {
|
||||
await setEditorContents([
|
||||
{ insert: '清空前的内容' },
|
||||
{ insert: '\n' }
|
||||
])
|
||||
await updateData({
|
||||
clearTest: false
|
||||
})
|
||||
await page.callMethod('clear')
|
||||
await waitForFlag('data.clearTest', 2000)
|
||||
const delta = await getDelta()
|
||||
const ops = Array.isArray(delta.ops) ? delta.ops : []
|
||||
expect(ops.length <= 1).toBe(true)
|
||||
if (ops.length === 1) {
|
||||
expect(ops[0].insert).toBe('\n')
|
||||
}
|
||||
})
|
||||
|
||||
it('undo-redo', async () => {
|
||||
await setEditorContents([
|
||||
{ insert: '撤销重做验证' },
|
||||
{ insert: '\n' }
|
||||
])
|
||||
await updateData({
|
||||
undoTest: false,
|
||||
redoTest: false
|
||||
})
|
||||
await page.callMethod('insertDivider')
|
||||
await page.waitFor(500)
|
||||
await page.callMethod('undo')
|
||||
await waitForFlag('data.undoTest', 2000)
|
||||
await page.callMethod('redo')
|
||||
await waitForFlag('data.redoTest', 2000)
|
||||
})
|
||||
|
||||
it('insertImage', async () => {
|
||||
await updateData({
|
||||
insertImageTest: false
|
||||
})
|
||||
await page.callMethod('insertImage', 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png')
|
||||
await waitForFlag('data.insertImageTest', 5000)
|
||||
const delta = await getDelta()
|
||||
const ops = Array.isArray(delta.ops) ? delta.ops : []
|
||||
const imageOp = ops.find(item => item.insert && typeof item.insert === 'object' && item.insert.image)
|
||||
expect(Boolean(imageOp)).toBe(true)
|
||||
})
|
||||
|
||||
it('insertImage-screenshot', async () => {
|
||||
await updateData({
|
||||
insertImageTest: false
|
||||
})
|
||||
await page.callMethod('insertImage', 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png')
|
||||
await waitForFlag('data.insertImageTest', 5000)
|
||||
await setBlur()
|
||||
const waitTime = process.env.uniTestPlatformInfo.includes('firefox') ? 5000 : 2000
|
||||
await page.waitFor(waitTime)
|
||||
expect(await program.screenshot({
|
||||
fullPage: true
|
||||
})).toSaveImageSnapshot()
|
||||
})
|
||||
|
||||
it('removeFormat', async () => {
|
||||
await setEditorContents([
|
||||
{
|
||||
insert: '设置字体样式',
|
||||
attributes: {
|
||||
bold: true,
|
||||
color: '#3553ff'
|
||||
}
|
||||
},
|
||||
{ insert: '\n' }
|
||||
])
|
||||
await updateData({
|
||||
removeFormatTest: false
|
||||
})
|
||||
await page.callMethod('removeFormat')
|
||||
await waitForFlag('data.removeFormatTest', 2000)
|
||||
})
|
||||
})
|
||||
文件差异内容过多而无法显示
加载差异
文件差异因一行或多行过长而隐藏
@@ -0,0 +1,165 @@
|
||||
<template>
|
||||
<!-- #ifdef APP -->
|
||||
<view class="preview-page preview-page-app">
|
||||
<web-view id="editor-preview-webview" :class="appPreviewLoaded ? 'preview-webview preview-webview-visible' : 'preview-webview preview-webview-hidden'" src="about:blank"></web-view>
|
||||
<view v-if="!appPreviewLoaded" class="preview-loading-mask">
|
||||
<text class="preview-loading-text">预览加载中...</text>
|
||||
</view>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
<!-- #ifndef APP -->
|
||||
<view class="preview-page preview-page-rich">
|
||||
<rich-text class="preview-rich-text" :nodes="previewHtml"></rich-text>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
const DEFAULT_PREVIEW_HTML = '<p style="color:#94a3b8;">暂无内容</p>'
|
||||
const previewHtml = ref(DEFAULT_PREVIEW_HTML)
|
||||
|
||||
let previewStorageKey = 'editor-preview-html'
|
||||
const appPreviewLoaded = ref(false)
|
||||
let previewWebviewElement: UniWebViewElement | null = null
|
||||
|
||||
const BLOCKQUOTE_STYLE = 'margin:12px 0;padding:12px 16px;border-left:4px solid #cbd5e1;background:#ffffff;color:#475569;'
|
||||
const PRE_STYLE = 'margin:12px 0;padding:12px;border-radius:10px;background:#0f172a;color:#e2e8f0;overflow:auto;white-space:pre-wrap;word-break:break-word;font-family:Menlo,Monaco,Consolas,monospace;'
|
||||
const INLINE_CODE_STYLE = 'padding:2px 6px;border-radius:6px;background:#e2e8f0;color:#0f172a;font-family:Menlo,Monaco,Consolas,monospace;'
|
||||
|
||||
function appendTagStyle(html: string, tagName: string, style: string) : string {
|
||||
const tagWithStylePattern = new RegExp(`<${tagName}([^>]*)style="([^"]*)"([^>]*)>`, 'gi')
|
||||
const tagPattern = new RegExp(`<${tagName}(?![^>]*style=)([^>]*)>`, 'gi')
|
||||
let nextHtml = html.replace(tagWithStylePattern, `<${tagName}$1style="$2;${style}"$3>`)
|
||||
nextHtml = nextHtml.replace(tagPattern, `<${tagName}$1 style="${style}">`)
|
||||
return nextHtml
|
||||
}
|
||||
|
||||
function appendClassStyle(html: string, className: string, style: string) : string {
|
||||
const classWithStylePattern = new RegExp(`<([a-z0-9-]+)([^>]*)class="([^"]*${className}[^"]*)"([^>]*)style="([^"]*)"([^>]*)>`, 'gi')
|
||||
const classPattern = new RegExp(`<([a-z0-9-]+)(?![^>]*style=)([^>]*)class="([^"]*${className}[^"]*)"([^>]*)>`, 'gi')
|
||||
let nextHtml = html.replace(classWithStylePattern, `<$1$2class="$3"$4style="$5;${style}"$6>`)
|
||||
nextHtml = nextHtml.replace(classPattern, `<$1$2class="$3"$4 style="${style}">`)
|
||||
return nextHtml
|
||||
}
|
||||
|
||||
function enhancePreviewHtml(html: string) : string {
|
||||
let nextHtml = html
|
||||
nextHtml = appendTagStyle(nextHtml, 'blockquote', BLOCKQUOTE_STYLE)
|
||||
nextHtml = appendTagStyle(nextHtml, 'pre', PRE_STYLE)
|
||||
nextHtml = appendTagStyle(nextHtml, 'code', INLINE_CODE_STYLE)
|
||||
nextHtml = appendClassStyle(nextHtml, 'ql-blockquote', BLOCKQUOTE_STYLE)
|
||||
nextHtml = appendClassStyle(nextHtml, 'ql-code-block', PRE_STYLE)
|
||||
nextHtml = appendClassStyle(nextHtml, 'code-block', PRE_STYLE)
|
||||
return nextHtml
|
||||
}
|
||||
|
||||
function normalizePreviewHtml(html: string) : string {
|
||||
const trimmedHtml = html.trim()
|
||||
if (trimmedHtml == '') {
|
||||
return DEFAULT_PREVIEW_HTML
|
||||
}
|
||||
return enhancePreviewHtml(trimmedHtml)
|
||||
}
|
||||
|
||||
function buildPreviewDocument(html: string) : string {
|
||||
return `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><style>html,body{margin:0;padding:0;background:#f8fafc;color:#1f2937;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;}body{padding:20px 16px 28px;line-height:1.7;word-break:break-word;}img,video{max-width:100%;height:auto;}pre,.ql-code-block,.code-block{${PRE_STYLE}}blockquote,.ql-blockquote{${BLOCKQUOTE_STYLE}}code{${INLINE_CODE_STYLE}}table{width:100%;border-collapse:collapse;}td,th{border:1px solid #dbe3eb;padding:8px;}a{color:#2563eb;text-decoration:none;}</style></head><body>${html}</body></html>`
|
||||
}
|
||||
|
||||
function readPreviewHtml(storageKey: string) : string {
|
||||
const storageValue = uni.getStorageSync(storageKey)
|
||||
if (typeof storageValue == 'string') {
|
||||
return normalizePreviewHtml(storageValue as string)
|
||||
}
|
||||
return DEFAULT_PREVIEW_HTML
|
||||
}
|
||||
|
||||
function loadAppPreview() {
|
||||
// #ifdef APP
|
||||
if (previewWebviewElement == null) {
|
||||
previewWebviewElement = uni.getElementById('editor-preview-webview') as UniWebViewElement | null
|
||||
}
|
||||
const webviewElement = previewWebviewElement
|
||||
if (webviewElement == null) {
|
||||
return
|
||||
}
|
||||
webviewElement.loadData({
|
||||
data: buildPreviewDocument(previewHtml.value)
|
||||
})
|
||||
appPreviewLoaded.value = true
|
||||
// #endif
|
||||
}
|
||||
|
||||
onLoad((options: OnLoadOptions) => {
|
||||
const storageKeyValue = options['storageKey']
|
||||
if (storageKeyValue != null) {
|
||||
const nextStorageKey = storageKeyValue as string
|
||||
if (nextStorageKey.length > 0) {
|
||||
previewStorageKey = nextStorageKey
|
||||
}
|
||||
}
|
||||
previewHtml.value = readPreviewHtml(previewStorageKey)
|
||||
})
|
||||
|
||||
onReady(() => {
|
||||
loadAppPreview()
|
||||
})
|
||||
|
||||
onUnload(() => {
|
||||
uni.removeStorageSync(previewStorageKey)
|
||||
previewWebviewElement = null
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.preview-page {
|
||||
background-color: #f8fafc;
|
||||
}
|
||||
|
||||
.preview-page-app {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.preview-page-rich {
|
||||
padding-top: 16px;
|
||||
padding-right: 16px;
|
||||
padding-bottom: 28px;
|
||||
padding-left: 16px;
|
||||
}
|
||||
|
||||
.preview-webview {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.preview-webview-hidden {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.preview-webview-visible {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.preview-loading-mask {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background-color: #f8fafc;
|
||||
}
|
||||
|
||||
.preview-loading-text {
|
||||
color: #64748b;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.preview-rich-text {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,257 @@
|
||||
const PAGE_PATH = '/pages/component/editor/editor'
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isWeb = platformInfo.startsWith('web') || platformInfo.startsWith('h5')
|
||||
const isMP = platformInfo.startsWith('mp')
|
||||
const isiOS = platformInfo.startsWith('ios')
|
||||
const isAndroid = platformInfo.startsWith('android')
|
||||
const isSimulator = platformInfo.includes('模拟器')
|
||||
const isDom2 = process.env.UNI_APP_X_DOM2 === 'true'
|
||||
|
||||
describe('editor.uvue', () => {
|
||||
const infos = process.env.uniTestPlatformInfo.split(' ');
|
||||
const version = parseInt(infos[infos.length - 1]);
|
||||
|
||||
if (isAndroid && !isNaN(version) && version < 8) {
|
||||
it('skip', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
let page
|
||||
|
||||
const deviceShotOptions = {
|
||||
deviceShot: true,
|
||||
area: {
|
||||
x: 0,
|
||||
y: 0
|
||||
}
|
||||
}
|
||||
|
||||
async function loadPage() {
|
||||
page = await program.reLaunch(PAGE_PATH)
|
||||
await page.waitFor('view')
|
||||
await waitForData('data.readyCount', value => value >= 1, 5000)
|
||||
await waitForData('data.editorWidth', value => value > 0, 3000)
|
||||
}
|
||||
|
||||
async function waitForData(path, matcher, timeout = 3000) {
|
||||
const start = Date.now()
|
||||
await page.waitFor(async () => {
|
||||
const value = await page.data(path)
|
||||
return matcher(value) || (Date.now() - start > timeout)
|
||||
})
|
||||
}
|
||||
|
||||
async function getEditorTapPoint(offsetX = null, offsetY = null) {
|
||||
const editorX = await page.data('data.editorX')
|
||||
const editorY = await page.data('data.editorY')
|
||||
const editorWidth = await page.data('data.editorWidth')
|
||||
const editorHeight = await page.data('data.editorHeight')
|
||||
return {
|
||||
x: editorX + (offsetX != null ? offsetX : editorWidth / 2),
|
||||
y: editorY + (offsetY != null ? offsetY : editorHeight / 2)
|
||||
}
|
||||
}
|
||||
|
||||
async function tapEditor(offsetX = null, offsetY = null) {
|
||||
const point = await getEditorTapPoint(offsetX, offsetY)
|
||||
await program.tap(point)
|
||||
if (isiOS) {
|
||||
// ios 模拟器卡
|
||||
await page.waitFor(1500)
|
||||
} else {
|
||||
await page.waitFor(600)
|
||||
}
|
||||
}
|
||||
|
||||
async function resetEditorProps() {
|
||||
const readyCount = await page.data('data.readyCount')
|
||||
await page.callMethod('onReadOnlyChange', false)
|
||||
await page.callMethod('onTypeChange', 0)
|
||||
await page.callMethod('onPlaceholderChange', '请输入内容,placeholder 仅初始化生效')
|
||||
await page.callMethod('onDraftShowImgSizeChange', true)
|
||||
await page.callMethod('onDraftShowImgToolbarChange', true)
|
||||
await page.callMethod('onDraftShowImgResizeChange', true)
|
||||
await page.callMethod('rebuildEditor')
|
||||
await waitForData('data.readyCount', value => value > readyCount, 5000)
|
||||
await waitForData('data.readOnly', value => value === false, 3000)
|
||||
await waitForData('data.editorType', value => value === null, 3000)
|
||||
await page.callMethod('clearEditor')
|
||||
if (!isWeb) {
|
||||
await page.callMethod('hideKeyboardForTest')
|
||||
await waitForData('data.keyboardHeight', value => value === 0, 3000)
|
||||
}
|
||||
await page.waitFor(600)
|
||||
}
|
||||
|
||||
async function hideKeyboard() {
|
||||
if (isWeb) {
|
||||
return
|
||||
}
|
||||
await page.callMethod('blurEditor')
|
||||
await page.callMethod('hideKeyboardForTest')
|
||||
await waitForData('data.keyboardHeight', value => value === 0, 3000)
|
||||
await page.waitFor(600)
|
||||
}
|
||||
|
||||
async function assertKeyboardHeightChange() {
|
||||
if (isWeb) {
|
||||
return
|
||||
}
|
||||
const beforeKeyboardHeight = await page.data('data.keyboardHeight')
|
||||
const beforeKeyboardHeightChangeCount = await page.data('data.keyboardHeightChangeCount')
|
||||
await tapEditor()
|
||||
// TODO 不再依赖键盘高度变化了,改为设备截图判断;安卓上运行自动化测试时 onKeyboardChagne 不触发
|
||||
/* await waitForData('data.keyboardHeight', value => value > beforeKeyboardHeight, 5000)
|
||||
await waitForData('data.keyboardHeightChangeCount', value => value > beforeKeyboardHeightChangeCount, 5000)
|
||||
expect(await page.data('data.keyboardHeight')).toBeGreaterThan(beforeKeyboardHeight)
|
||||
expect(await page.data('data.keyboardHeightChangeCount')).toBeGreaterThan(beforeKeyboardHeightChangeCount) */
|
||||
}
|
||||
|
||||
async function screenshot(name, deviceShot = false) {
|
||||
const options = deviceShot ? {fullPage: true} : deviceShotOptions
|
||||
const image = await program.screenshot(options)
|
||||
expect(image).toSaveImageSnapshot({
|
||||
customSnapshotIdentifier() {
|
||||
return name
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function getInserImageSize() {
|
||||
await waitForData('data.insertImageWidth', value => value > 0, 2000)
|
||||
await waitForData('data.insertImageHeight', value => value > 0, 2000)
|
||||
const width = await page.data('data.insertImageWidth')
|
||||
const height = await page.data('data.insertImageHeight')
|
||||
if (width === 0) width = 50
|
||||
if (height === 0) width = 30
|
||||
return {
|
||||
width,
|
||||
height
|
||||
}
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
await loadPage()
|
||||
const windowInfo = await program.callUniMethod('getWindowInfo');
|
||||
deviceShotOptions.area.y = windowInfo.safeAreaInsets.top + 44
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetEditorProps()
|
||||
})
|
||||
|
||||
it('placeholder 截图', async () => {
|
||||
const readyCount = await page.data('data.readyCount')
|
||||
await page.callMethod('onPlaceholderChange', '自动化测试 editor placeHolder 修改')
|
||||
await page.callMethod('rebuildEditor')
|
||||
await waitForData('data.readyCount', value => value >= readyCount + 1, 2000)
|
||||
await page.waitFor(500)
|
||||
await screenshot('editor-props-placeholder')
|
||||
})
|
||||
|
||||
it('方法调用截图', async () => {
|
||||
await page.callMethod('insertSampleText')
|
||||
if (!isMP) {
|
||||
await page.callMethod('insertMention')
|
||||
}
|
||||
await page.waitFor(500)
|
||||
await screenshot('editor-event-invoke')
|
||||
})
|
||||
|
||||
it('setContents html', async () => {
|
||||
await page.callMethod('testSetContentsHtml')
|
||||
await page.waitFor(500)
|
||||
await screenshot('editor-setContents-html')
|
||||
})
|
||||
|
||||
if (!isMP) {
|
||||
it('readOnly 修改后截图', async () => {
|
||||
expect(await page.data('data.readOnly')).toBe(false)
|
||||
expect(await page.data('data.editorType')).toBeFalsy()
|
||||
await assertKeyboardHeightChange()
|
||||
await screenshot('editor-props-read-only-false', true)
|
||||
|
||||
await hideKeyboard()
|
||||
|
||||
await page.callMethod('onReadOnlyChange', true)
|
||||
await waitForData('data.readOnly', value => value === true, 3000)
|
||||
expect(await page.data('data.readOnly')).toBe(true)
|
||||
await screenshot('editor-props-read-only-true')
|
||||
await page.callMethod('onReadOnlyChange', false)
|
||||
await waitForData('data.readOnly', value => value === false, 3000)
|
||||
})
|
||||
|
||||
it('type 切换后截图', async () => {
|
||||
expect(await page.data('data.editorType')).toBeFalsy()
|
||||
expect(await page.callMethod('getTypeLabel')).toBe('null(聚焦弹键盘)')
|
||||
await assertKeyboardHeightChange()
|
||||
|
||||
await screenshot('editor-props-type-null', true)
|
||||
|
||||
await hideKeyboard()
|
||||
|
||||
await page.callMethod('onTypeChange', 1)
|
||||
await waitForData('data.editorType', value => value === 'none', 3000)
|
||||
expect(await page.data('data.editorType')).toBe('none')
|
||||
expect(await page.callMethod('getTypeLabel')).toBe('none(聚焦不弹键盘)')
|
||||
|
||||
await screenshot('editor-props-type-none')
|
||||
})
|
||||
|
||||
it('image 相关属性重建后点击 image 截图', async () => {
|
||||
const previousReadyCount = await page.data('data.readyCount')
|
||||
|
||||
await page.callMethod('onDraftShowImgSizeChange', true)
|
||||
await page.callMethod('onDraftShowImgToolbarChange', true)
|
||||
await page.callMethod('onDraftShowImgResizeChange', true)
|
||||
await page.callMethod('rebuildEditor')
|
||||
await waitForData('data.readyCount', value => value >= previousReadyCount + 1, 8000)
|
||||
|
||||
await page.callMethod('insertSampleImage')
|
||||
let imageSize = await getInserImageSize()
|
||||
await tapEditor(imageSize.width/2, imageSize.height/2)
|
||||
|
||||
await screenshot('editor-props-image-controls-true')
|
||||
|
||||
const nextReadyCount = await page.data('data.readyCount')
|
||||
await page.callMethod('blurEditor')
|
||||
await waitForData('data.blurCount', value => value > 0, 3000)
|
||||
await page.callMethod('clearEditor')
|
||||
await page.waitFor(500)
|
||||
await page.callMethod('onDraftShowImgSizeChange', false)
|
||||
await page.callMethod('onDraftShowImgToolbarChange', false)
|
||||
await page.callMethod('onDraftShowImgResizeChange', false)
|
||||
await page.callMethod('rebuildEditor')
|
||||
await waitForData('data.readyCount', value => value >= nextReadyCount + 1, 8000)
|
||||
|
||||
expect(await page.data('data.appliedShowImgSize')).toBe(false)
|
||||
expect(await page.data('data.appliedShowImgToolbar')).toBe(false)
|
||||
expect(await page.data('data.appliedShowImgResize')).toBe(false)
|
||||
|
||||
await page.callMethod('insertSampleImage')
|
||||
imageSize = await getInserImageSize()
|
||||
await tapEditor(imageSize.width/2, imageSize.height/2)
|
||||
|
||||
await screenshot('editor-props-image-controls-false')
|
||||
})
|
||||
}
|
||||
|
||||
it('事件只校验触发次数', async () => {
|
||||
await resetEditorProps()
|
||||
const readyCount = await page.data('data.readyCount')
|
||||
const inputCount = await page.data('data.inputCount')
|
||||
const statusChangeCount = await page.data('data.statusChangeCount')
|
||||
|
||||
await page.callMethod('insertSampleText')
|
||||
await waitForData('data.inputCount', value => value > inputCount, 3000)
|
||||
|
||||
await page.callMethod('toggleBold')
|
||||
await waitForData('data.statusChangeCount', value => value > statusChangeCount, 3000)
|
||||
|
||||
expect(await page.data('data.readyCount')).toBeGreaterThanOrEqual(readyCount)
|
||||
expect(await page.data('data.inputCount')).toBeGreaterThan(inputCount)
|
||||
expect(await page.data('data.statusChangeCount')).toBeGreaterThan(statusChangeCount)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,612 @@
|
||||
<template>
|
||||
<view class="page-root">
|
||||
<view class="preview-panel">
|
||||
<view class="preview-header">
|
||||
<text class="uni-title-text">editor 属性示例</text>
|
||||
</view>
|
||||
|
||||
<view class="preview-state-row">
|
||||
<text class="preview-state-text">read-only: {{ data.readOnly }}</text>
|
||||
<!-- #ifndef MP -->
|
||||
<text class="preview-state-text">type: {{ getTypeLabel() }}</text>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
|
||||
<view class="preview-state-row">
|
||||
<view v-if="showLogs">
|
||||
<text class="preview-state-text">X: {{ data.editorX }}</text>
|
||||
<text class="preview-state-text">Y: {{ data.editorY }}</text>
|
||||
<text class="preview-state-text">width: {{ data.editorWidth }}</text>
|
||||
<text class="preview-state-text">height: {{ data.editorHeight }}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="preview-editor-box">
|
||||
<editor
|
||||
v-if="data.showEditor"
|
||||
id="editor-props-demo"
|
||||
class="preview-editor"
|
||||
:read-only="data.readOnly"
|
||||
:placeholder="data.appliedPlaceholder"
|
||||
:show-img-size="data.appliedShowImgSize"
|
||||
:show-img-toolbar="data.appliedShowImgToolbar"
|
||||
:show-img-resize="data.appliedShowImgResize"
|
||||
:type="data.editorType"
|
||||
@ready="onEditorReady"
|
||||
@focus="onEditorFocus"
|
||||
@blur="onEditorBlur"
|
||||
@input="onEditorInput"
|
||||
@statuschange="onStatusChange"
|
||||
/>
|
||||
</view>
|
||||
|
||||
<view class="preview-toolbar">
|
||||
<view class="preview-toolbar-item" @tap="insertSampleText">
|
||||
<text class="preview-toolbar-icon iconfont">A</text>
|
||||
</view>
|
||||
<view class="preview-toolbar-item" @tap="insertSampleImage">
|
||||
<text class="preview-toolbar-icon iconfont">{{""}}</text>
|
||||
</view>
|
||||
<view :class="data.boldActive ? 'preview-toolbar-item preview-toolbar-item-active' : 'preview-toolbar-item'" @tap="toggleBold">
|
||||
<text :class="data.boldActive ? 'preview-toolbar-icon preview-toolbar-icon-active iconfont' : 'preview-toolbar-icon iconfont'">{{""}}</text>
|
||||
</view>
|
||||
<view :class="data.alignCenterActive ? 'preview-toolbar-item preview-toolbar-item-active' : 'preview-toolbar-item'" @tap="setAlignCenter">
|
||||
<text :class="data.alignCenterActive ? 'preview-toolbar-icon preview-toolbar-icon-active iconfont' : 'preview-toolbar-icon iconfont'">{{""}}</text>
|
||||
</view>
|
||||
<view class="preview-toolbar-item" @tap="removeFormat">
|
||||
<text class="preview-toolbar-icon iconfont">{{""}}</text>
|
||||
</view>
|
||||
<view class="preview-toolbar-item" @tap="clearEditor">
|
||||
<text class="preview-toolbar-icon iconfont">{{""}}</text>
|
||||
</view>
|
||||
<view class="preview-toolbar-item" @tap="blurEditor">
|
||||
<text class="preview-toolbar-icon iconfont">{{""}}</text>
|
||||
</view>
|
||||
<!-- #ifndef MP -->
|
||||
<view class="preview-toolbar-item" @tap="insertMention">
|
||||
<text class="preview-toolbar-icon iconfont">@</text>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<scroll-view class="props-scroll" direction="vertical">
|
||||
<view class="uni-padding-wrap">
|
||||
<view class="uni-title uni-common-mt">
|
||||
<text class="uni-title-text">可动态修改属性</text>
|
||||
</view>
|
||||
</view>
|
||||
<boolean-data :defaultValue="data.readOnly" title="read-only:设置编辑器为只读,可动态修改" @change="onReadOnlyChange"></boolean-data>
|
||||
<!-- #ifndef MP -->
|
||||
<enum-data :items="data.typeItems" title="type:null | none,控制聚焦时是否弹出键盘" @change="onTypeChange"></enum-data>
|
||||
<!-- #endif -->
|
||||
|
||||
<view class="uni-padding-wrap">
|
||||
<view class="uni-title uni-common-mt">
|
||||
<text class="uni-title-text">初始化生效属性</text>
|
||||
</view>
|
||||
<text class="uni-subtitle-text">placeholder 与 show-img-* 调整后不会直接作用到当前 editor,请点击下方按钮重建后查看。</text>
|
||||
</view>
|
||||
<input-data :defaultValue="data.draftPlaceholder" title="placeholder:提示信息,不可动态修改" type="text" @confirm="onPlaceholderChange"></input-data>
|
||||
<boolean-data :defaultValue="data.draftShowImgSize" title="show-img-size:点击图片时显示图片大小控件,不可动态修改" @change="onDraftShowImgSizeChange"></boolean-data>
|
||||
<boolean-data :defaultValue="data.draftShowImgToolbar" title="show-img-toolbar:点击图片时显示工具栏控件,不可动态修改" @change="onDraftShowImgToolbarChange"></boolean-data>
|
||||
<boolean-data :defaultValue="data.draftShowImgResize" title="show-img-resize:点击图片时显示修改尺寸控件,不可动态修改" @change="onDraftShowImgResizeChange"></boolean-data>
|
||||
|
||||
<view class="uni-padding-wrap uni-common-mt">
|
||||
<view class="uni-title">
|
||||
<text class="uni-title-text">当前应用中的 editor 配置</text>
|
||||
</view>
|
||||
<text class="uni-subtitle-text">placeholder:{{ data.appliedPlaceholder }}</text>
|
||||
<text class="uni-subtitle-text">show-img-size:{{ data.appliedShowImgSize }}</text>
|
||||
<text class="uni-subtitle-text">show-img-toolbar:{{ data.appliedShowImgToolbar }}</text>
|
||||
<text class="uni-subtitle-text">show-img-resize:{{ data.appliedShowImgResize }}</text>
|
||||
<button type="primary" size="mini" class="action-button apply-button" @tap="rebuildEditor">重建 editor 并应用初始化属性</button>
|
||||
</view>
|
||||
|
||||
<view v-if="showLogs" class="uni-padding-wrap uni-common-mt">
|
||||
<view class="uni-title">
|
||||
<text class="uni-title-text">事件示例</text>
|
||||
</view>
|
||||
<text class="uni-subtitle-text">@ready:{{ data.readyCount }}</text>
|
||||
<text class="uni-subtitle-text">@focus:{{ data.focusCount }}</text>
|
||||
<text class="uni-subtitle-text">@blur:{{ data.blurCount }}</text>
|
||||
<text class="uni-subtitle-text">@input:{{ data.inputCount }}</text>
|
||||
<text class="uni-subtitle-text">@statuschange:{{ data.statusChangeCount }}</text>
|
||||
<text class="uni-subtitle-text">最近状态:{{ data.statusSummary }}</text>
|
||||
</view>
|
||||
|
||||
<view v-if="showLogs" class="uni-padding-wrap uni-common-mt uni-common-mb">
|
||||
<view class="uni-title event-header-row">
|
||||
<text class="uni-title-text">事件日志</text>
|
||||
<button size="mini" @tap="clearLogs">清空日志</button>
|
||||
</view>
|
||||
<view class="log-box" v-if="data.logs.length > 0">
|
||||
<text v-for="(item, index) in data.logs" :key="index" class="log-text">{{ item }}</text>
|
||||
</view>
|
||||
<text v-else class="uni-subtitle-text">等待事件触发...</text>
|
||||
</view>
|
||||
</scroll-view>
|
||||
|
||||
<navigator style="margin-top: 20px;" url="./editor-edit">
|
||||
<button>富文本编辑模板</button>
|
||||
</navigator>
|
||||
|
||||
<bottom-safe-area />
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
import { ItemType } from '@/components/enum-data/enum-data-types'
|
||||
|
||||
type EditorPropsPageData = {
|
||||
showEditor: boolean
|
||||
readOnly: boolean
|
||||
editorType: string | null
|
||||
draftPlaceholder: string
|
||||
appliedPlaceholder: string
|
||||
draftShowImgSize: boolean
|
||||
appliedShowImgSize: boolean
|
||||
draftShowImgToolbar: boolean
|
||||
appliedShowImgToolbar: boolean
|
||||
draftShowImgResize: boolean
|
||||
appliedShowImgResize: boolean
|
||||
editorCtx: EditorContext | null
|
||||
readyCount: number
|
||||
focusCount: number
|
||||
blurCount: number
|
||||
inputCount: number
|
||||
statusChangeCount: number
|
||||
boldActive: boolean
|
||||
alignCenterActive: boolean
|
||||
statusSummary: string
|
||||
logs: string[]
|
||||
typeItems: ItemType[]
|
||||
editorX: number
|
||||
editorY: number
|
||||
editorWidth: number
|
||||
editorHeight: number
|
||||
insertImageWidth: number
|
||||
insertImageHeight: number
|
||||
}
|
||||
|
||||
const SAMPLE_TEXT = '这是 editor 示例文本。你可以继续输入内容,也可以滚动下方属性区实时调整 editor 状态。'
|
||||
function createTypeItems(selectedValue: number): ItemType[] {
|
||||
return [
|
||||
{ value: 0, name: 'null:聚焦弹出键盘', checked: selectedValue == 0 },
|
||||
{ value: 1, name: 'none:聚焦不弹出键盘', checked: selectedValue == 1 }
|
||||
]
|
||||
}
|
||||
|
||||
const showLogs = false
|
||||
|
||||
const initialData: EditorPropsPageData = {
|
||||
showEditor: true,
|
||||
readOnly: false,
|
||||
editorType: null,
|
||||
draftPlaceholder: '请输入内容,placeholder 仅初始化生效',
|
||||
appliedPlaceholder: '请输入内容,placeholder 仅初始化生效',
|
||||
draftShowImgSize: true,
|
||||
appliedShowImgSize: true,
|
||||
draftShowImgToolbar: true,
|
||||
appliedShowImgToolbar: true,
|
||||
draftShowImgResize: true,
|
||||
appliedShowImgResize: true,
|
||||
editorCtx: null,
|
||||
readyCount: 0,
|
||||
focusCount: 0,
|
||||
blurCount: 0,
|
||||
inputCount: 0,
|
||||
statusChangeCount: 0,
|
||||
boldActive: false,
|
||||
alignCenterActive: false,
|
||||
statusSummary: '等待 @statuschange',
|
||||
logs: [],
|
||||
typeItems: createTypeItems(0),
|
||||
editorX: 0,
|
||||
editorY: 0,
|
||||
editorWidth: 0,
|
||||
editorHeight: 0,
|
||||
insertImageWidth: 0,
|
||||
insertImageHeight: 0
|
||||
}
|
||||
|
||||
const data = reactive<EditorPropsPageData>(initialData)
|
||||
|
||||
function appendLog(tag: string, detail: string) {
|
||||
const entry = `${new Date().toLocaleTimeString()} [${tag}] ${detail}`
|
||||
data.logs.unshift(entry)
|
||||
if (data.logs.length > 12) {
|
||||
data.logs.pop()
|
||||
}
|
||||
}
|
||||
|
||||
function getTypeLabel(): string {
|
||||
return data.editorType == 'none' ? 'none(聚焦不弹键盘)' : 'null(聚焦弹键盘)'
|
||||
}
|
||||
|
||||
function summarizeText(text: string): string {
|
||||
if (text.length <= 24) {
|
||||
return text
|
||||
}
|
||||
return `${text.substring(0, 24)}...`
|
||||
}
|
||||
|
||||
function syncTypeItems() {
|
||||
const selectedValue = data.editorType == 'none' ? 1 : 0
|
||||
data.typeItems = createTypeItems(selectedValue)
|
||||
}
|
||||
|
||||
function onReadOnlyChange(checked: boolean) {
|
||||
data.readOnly = checked
|
||||
appendLog('prop', `read-only => ${checked}`)
|
||||
}
|
||||
|
||||
function onTypeChange(value: number) {
|
||||
data.editorType = value == 1 ? 'none' : null
|
||||
syncTypeItems()
|
||||
appendLog('prop', `type => ${data.editorType == null ? 'null' : data.editorType}`)
|
||||
}
|
||||
|
||||
function onPlaceholderChange(value: string) {
|
||||
data.draftPlaceholder = value
|
||||
appendLog('draft', `placeholder 待应用 => ${data.draftPlaceholder}`)
|
||||
}
|
||||
|
||||
function onDraftShowImgSizeChange(checked: boolean) {
|
||||
data.draftShowImgSize = checked
|
||||
appendLog('draft', `show-img-size 待应用 => ${checked}`)
|
||||
}
|
||||
|
||||
function onDraftShowImgToolbarChange(checked: boolean) {
|
||||
data.draftShowImgToolbar = checked
|
||||
appendLog('draft', `show-img-toolbar 待应用 => ${checked}`)
|
||||
}
|
||||
|
||||
function onDraftShowImgResizeChange(checked: boolean) {
|
||||
data.draftShowImgResize = checked
|
||||
appendLog('draft', `show-img-resize 待应用 => ${checked}`)
|
||||
}
|
||||
|
||||
function rebuildEditor() {
|
||||
data.appliedPlaceholder = data.draftPlaceholder
|
||||
data.appliedShowImgSize = data.draftShowImgSize
|
||||
data.appliedShowImgToolbar = data.draftShowImgToolbar
|
||||
data.appliedShowImgResize = data.draftShowImgResize
|
||||
data.editorCtx = null
|
||||
data.boldActive = false
|
||||
data.alignCenterActive = false
|
||||
data.statusSummary = '等待 @statuschange'
|
||||
data.showEditor = false
|
||||
setTimeout(() => {
|
||||
data.showEditor = true
|
||||
}, 0)
|
||||
appendLog('rebuild', '已重建 editor,初始化属性已重新应用')
|
||||
}
|
||||
|
||||
function updateEditorRect() {
|
||||
// #ifndef MP
|
||||
const editorElement = uni.getElementById('editor-props-demo')
|
||||
const editorRect = editorElement?.getBoundingClientRect()
|
||||
if (editorRect != null) {
|
||||
data.editorX = editorRect.x
|
||||
data.editorY = editorRect.y
|
||||
data.editorWidth = editorRect.width
|
||||
data.editorHeight = editorRect.height
|
||||
// #ifdef APP-IOS || APP-HARMONY || APP-ANDROID
|
||||
const systemInfo = uni.getSystemInfoSync()
|
||||
data.editorY += systemInfo.safeAreaInsets.top + 44
|
||||
// #endif
|
||||
}
|
||||
// #endif
|
||||
// #ifdef MP
|
||||
wx.createSelectorQuery().select('#editor-props-demo').boundingClientRect(function(editorRect){
|
||||
data.editorX = editorRect.top
|
||||
data.editorY = editorRect.left
|
||||
data.editorWidth = editorRect.width
|
||||
data.editorHeight = editorRect.height
|
||||
|
||||
const windowInfo = uni.getWindowInfo()
|
||||
data.editorY += windowInfo.safeAreaInsets.top + 44
|
||||
}).exec()
|
||||
// #endif
|
||||
}
|
||||
|
||||
function onEditorReady() {
|
||||
const options: CreateEditorContextAsyncOptions = {
|
||||
id: 'editor-props-demo',
|
||||
success: (context: EditorContext) => {
|
||||
data.editorCtx = context
|
||||
data.readyCount += 1
|
||||
updateEditorRect()
|
||||
appendLog('ready', '编辑器初始化完成')
|
||||
},
|
||||
fail: (error: UniError) => {
|
||||
appendLog('error', `createEditorContextAsync fail: ${error.errMsg}`)
|
||||
}
|
||||
}
|
||||
uni.createEditorContextAsync(options)
|
||||
}
|
||||
|
||||
function onEditorFocus(event: UniEditorFocusEvent) {
|
||||
data.focusCount += 1
|
||||
appendLog('focus', `text=${summarizeText(event.detail.text)}`)
|
||||
}
|
||||
|
||||
function onEditorBlur(event: UniEditorBlurEvent) {
|
||||
data.blurCount += 1
|
||||
appendLog('blur', `text=${summarizeText(event.detail.text)}`)
|
||||
}
|
||||
|
||||
function onEditorInput(event: UniEditorInputEvent) {
|
||||
data.inputCount += 1
|
||||
appendLog('input', `text=${summarizeText(event.detail.text)}`)
|
||||
}
|
||||
|
||||
function onStatusChange(event: UniEditorStatusChangeEvent) {
|
||||
data.statusChangeCount += 1
|
||||
data.boldActive = event.detail.bold == true
|
||||
data.alignCenterActive = event.detail.align == 'center'
|
||||
const align = event.detail.align != null ? event.detail.align : 'default'
|
||||
const fontSize = event.detail.fontSize != null ? event.detail.fontSize : 'default'
|
||||
data.statusSummary = `bold=${event.detail.bold == true}, align=${align}, fontSize=${fontSize}`
|
||||
appendLog('statuschange', data.statusSummary)
|
||||
}
|
||||
|
||||
function insertSampleText() {
|
||||
data.editorCtx?.insertText({
|
||||
text: SAMPLE_TEXT,
|
||||
success: () => {
|
||||
appendLog('action', '已插入示例文本')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const src = 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png'
|
||||
function insertImage() {
|
||||
data.editorCtx?.insertImage({
|
||||
src,
|
||||
alt: '示例图片',
|
||||
success: () => {
|
||||
appendLog('action', '已插入示例图片,请点击图片验证 show-img-*')
|
||||
},
|
||||
fail: () => {
|
||||
appendLog('error', '插入示例图片失败')
|
||||
}
|
||||
})
|
||||
}
|
||||
function insertSampleImage() {
|
||||
uni.getImageInfo({
|
||||
src,
|
||||
success(res) {
|
||||
console.log('res: ',res);
|
||||
data.insertImageWidth = res.width
|
||||
data.insertImageHeight = res.height
|
||||
appendLog('network', `图片加载成功: ${summarizeText(src)}`)
|
||||
insertImage()
|
||||
},
|
||||
fail() {
|
||||
insertImage()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const insertMention = () => {
|
||||
data.editorCtx?.insertMention({
|
||||
id: '000',
|
||||
name: 'uni-app x',
|
||||
success: (res: any) => {
|
||||
console.log('insert mention success')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function toggleBold() {
|
||||
data.editorCtx?.format('bold', null)
|
||||
appendLog('action', `请求切换加粗 => ${!data.boldActive}`)
|
||||
}
|
||||
|
||||
function setAlignCenter() {
|
||||
data.editorCtx?.format('align', 'center')
|
||||
appendLog('action', '请求设置居中对齐')
|
||||
}
|
||||
|
||||
function removeFormat() {
|
||||
data.editorCtx?.removeFormat({
|
||||
success: () => {
|
||||
appendLog('action', '已清除当前样式')
|
||||
},
|
||||
fail: () => {
|
||||
appendLog('error', '清除样式失败')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function clearEditor() {
|
||||
data.editorCtx?.clear({
|
||||
success: () => {
|
||||
appendLog('action', '已清空编辑器内容')
|
||||
},
|
||||
fail: () => {
|
||||
appendLog('error', '清空编辑器失败')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function blurEditor() {
|
||||
data.editorCtx?.blur({
|
||||
success: () => {
|
||||
appendLog('action', '已主动触发失焦')
|
||||
},
|
||||
fail: () => {
|
||||
appendLog('error', '主动失焦失败')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function hideKeyboardForTest() {
|
||||
uni.hideKeyboard()
|
||||
appendLog('keyboard', '已请求隐藏键盘')
|
||||
}
|
||||
|
||||
function clearLogs() {
|
||||
data.logs.splice(0, data.logs.length)
|
||||
}
|
||||
|
||||
function testSetContentsHtml() {
|
||||
data.editorCtx?.setContents({
|
||||
html: '<p style="text-align:center;"><strong>Hello,uni-app x</strong></p><p>editor的setContents接口示例</p>',
|
||||
success: () => {
|
||||
appendLog('action', '成功设置富文本内容')
|
||||
},
|
||||
fail: (error: any) => {
|
||||
appendLog('error', `setContents fail: ${JSON.stringify(error)}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
data,
|
||||
getTypeLabel,
|
||||
onReadOnlyChange,
|
||||
onTypeChange,
|
||||
onPlaceholderChange,
|
||||
onDraftShowImgSizeChange,
|
||||
onDraftShowImgToolbarChange,
|
||||
onDraftShowImgResizeChange,
|
||||
rebuildEditor,
|
||||
insertSampleText,
|
||||
insertMention,
|
||||
insertSampleImage,
|
||||
toggleBold,
|
||||
setAlignCenter,
|
||||
removeFormat,
|
||||
clearEditor,
|
||||
blurEditor,
|
||||
hideKeyboardForTest,
|
||||
updateEditorRect,
|
||||
testSetContentsHtml
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
@import './editor-icon.css';
|
||||
|
||||
.page-root {
|
||||
flex-direction: column;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.preview-panel {
|
||||
flex-direction: column;
|
||||
padding: 10px;
|
||||
background-color: #ffffff;
|
||||
border-bottom-width: 1px;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
.preview-header {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.preview-state-row {
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 6px;
|
||||
}
|
||||
|
||||
.preview-toolbar {
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 10px;
|
||||
padding-top: 8px;
|
||||
padding-right: 8px;
|
||||
padding-bottom: 8px;
|
||||
border-radius: 6px;
|
||||
background-color: #f5f6f8;
|
||||
}
|
||||
|
||||
.preview-toolbar-item {
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
margin-top: 8px;
|
||||
margin-left: 8px;
|
||||
border-radius: 6px;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
|
||||
.preview-toolbar-item-active {
|
||||
background-color: #e6f0ff;
|
||||
}
|
||||
|
||||
.preview-toolbar-icon {
|
||||
color: #333333;
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.preview-toolbar-icon-active {
|
||||
color: #007aff;
|
||||
}
|
||||
|
||||
.preview-state-text {
|
||||
margin-right: 12px;
|
||||
color: #666666;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.preview-editor-box {
|
||||
margin-top: 10px;
|
||||
height: 150px;
|
||||
border-width: 1px;
|
||||
border-style: solid;
|
||||
border-color: rgba(0, 0, 0, 0.08);
|
||||
border-radius: 6px;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
|
||||
.preview-editor {
|
||||
height: 150px;
|
||||
min-height: 0px;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.props-scroll {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.button-list {
|
||||
flex-direction: column;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.action-button {
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.apply-button {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.event-header-row {
|
||||
flex-direction: row;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.log-box {
|
||||
flex-direction: column;
|
||||
margin-top: 10px;
|
||||
padding: 10px;
|
||||
border-radius: 6px;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
|
||||
.log-text {
|
||||
margin-top: 6px;
|
||||
color: #333333;
|
||||
font-size: 12px;
|
||||
line-height: 18px;
|
||||
}
|
||||
</style>
|
||||
在新工单中引用
屏蔽一个用户