diff --git a/CHANGELOG.md b/CHANGELOG.md index b7a6758f9ceb465d65c8891a1efc068e0d4b8f71..4aa06413ebbfc6bd0fd0f53c26514570d1c3ae99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ ## [Unreleased] +### Added + +- 富文本支持展开和收缩功能 + ## [0.0.33] - 2024-10-30 ### Added diff --git a/src/editor/html/html-editor.controller.ts b/src/editor/html/html-editor.controller.ts index 55e4f79a692840692205becf05817ff1f326ce30..18fe66bca4bdc292f5d98f8b9488b316ff361ae9 100644 --- a/src/editor/html/html-editor.controller.ts +++ b/src/editor/html/html-editor.controller.ts @@ -26,6 +26,22 @@ export class HtmlEditorController extends EditorController { */ public showToolbar: boolean = true; + /** + * 收缩时的高度 + * + * @type {number} + * @memberof HtmlEditorController + */ + public defaultHeight: number = 200; + + /** + * 是否显示伸缩按钮 + * + * @type {boolean} + * @memberof HtmlEditorController + */ + public showCollapse: boolean = true; + /** * @description 值模式(暂时只支持html模式,text模式存在问题) * @type {('text' | 'html')} @@ -70,6 +86,8 @@ export class HtmlEditorController extends EditorController { VALUEMODE, IMAGEMODE, MODULES, + DEFAULTHEIGHT, + SHOWCOLLAPSE, } = this.editorParams; if (uploadParams) { @@ -109,6 +127,12 @@ export class HtmlEditorController extends EditorController { MODULES, ) as IData; } + if (DEFAULTHEIGHT) { + this.defaultHeight = Number(DEFAULTHEIGHT); + } + if (SHOWCOLLAPSE) { + this.showCollapse = this.toBoolean(SHOWCOLLAPSE); + } } } } diff --git a/src/editor/html/quill-editor-preview/quill-editor-preview.scss b/src/editor/html/quill-editor-preview/quill-editor-preview.scss index 6c4ba5ec1f56cae5e0411f9260ddc988a4b51be6..e97b732b5b58ab130320e84a78c29b56cfee2dee 100644 --- a/src/editor/html/quill-editor-preview/quill-editor-preview.scss +++ b/src/editor/html/quill-editor-preview/quill-editor-preview.scss @@ -33,4 +33,14 @@ left: 0; } } + @include m('show-collapse'){ + @include when('collapse'){ + height: var(--default-height); + } + } +} +@include b('quill-preview-collapse-btn'){ + color: getCssVar(color,primary); + display: flex; + align-items: center; } \ No newline at end of file diff --git a/src/editor/html/quill-editor-preview/quill-editor-preview.tsx b/src/editor/html/quill-editor-preview/quill-editor-preview.tsx index c9ed4b7bf77bfbe80a6b1145cf569dccfc4c273d..b2d155726bf3355c262d154e359fdfd963a5bdff 100644 --- a/src/editor/html/quill-editor-preview/quill-editor-preview.tsx +++ b/src/editor/html/quill-editor-preview/quill-editor-preview.tsx @@ -1,5 +1,5 @@ /* eslint-disable import/no-extraneous-dependencies */ -import { defineComponent, onMounted, ref } from 'vue'; +import { defineComponent, nextTick, onMounted, ref, watch } from 'vue'; import { getHtmlProps, useNamespace } from '@ibiz-template/vue3-util'; import Quill from 'quill'; import { HtmlEditorController } from '../html-editor.controller'; @@ -8,7 +8,16 @@ import './quill-editor-preview.scss'; // eslint-disable-next-line @typescript-eslint/no-explicit-any const IBizQuillPreview: any = defineComponent({ name: 'IBizQuillPreview', - props: getHtmlProps(), + props: { + ...getHtmlProps(), + showCollapse: { + type: Boolean, + }, + defaultHeight: { + type: Number, + default: 200, + }, + }, setup(props, { emit }) { const ns = useNamespace('quill-preview'); const c: HtmlEditorController = props.controller; @@ -23,6 +32,15 @@ const IBizQuillPreview: any = defineComponent({ // 预览图片路径 const previewImage = ref(''); + // 是否完全展开 + const isCollapse = ref(true); + + // 伸缩内容高度 + const collapseHeight = ref('auto'); + + // 显示伸缩按钮 + const showCollapseBtn = ref(false); + const init = () => { if (!editorRef.value) { return; @@ -38,8 +56,45 @@ const IBizQuillPreview: any = defineComponent({ } }; + // 重置伸缩高度 + const resetCollapseHeight = () => { + if (isCollapse.value && editorRef.value) { + // 收缩时 + const height = (editorRef.value as HTMLElement).offsetHeight; + // 内容高度低于设置的默认高度时,拿内容高度作为伸缩高度 + if (height < props.defaultHeight) { + collapseHeight.value = `${height}px`; + showCollapseBtn.value = false; + } else { + collapseHeight.value = `${props.defaultHeight}px`; + showCollapseBtn.value = true; + } + } + }; + + // 监听img加载情况 + const watchHtmlImages = (container: HTMLElement | null) => { + if (!container) return; + const images = container.querySelectorAll('img'); + let totalHieght = 0; + images.forEach(image => { + image.addEventListener('load', () => { + const height = image.offsetHeight; + totalHieght += height; + if (totalHieght > props.defaultHeight) { + collapseHeight.value = `${props.defaultHeight}px`; + showCollapseBtn.value = true; + } + }); + }); + }; + onMounted(() => { init(); + nextTick(() => { + resetCollapseHeight(); + watchHtmlImages(editorRef.value); + }); }); const handleClick = () => { @@ -57,14 +112,49 @@ const IBizQuillPreview: any = defineComponent({ } }; + // 点击伸缩按钮 + const onClick = () => { + isCollapse.value = !isCollapse.value; + resetCollapseHeight(); + }; + + // 绘制伸缩按钮 + const renderCollapseBtn = () => { + return ( +
+ + {isCollapse.value + ? ibiz.i18n.t('editor.html.expand') + : ibiz.i18n.t('editor.html.collapse')} + + {isCollapse.value && } + {!isCollapse.value && } +
+ ); + }; + + watch( + () => props.value, + () => { + resetCollapseHeight(); + }, + { + immediate: true, + }, + ); + return { ns, lang, editorRef, previewImage, + isCollapse, + collapseHeight, + showCollapseBtn, handleClick, handleContentClick, handlePreviewClose, + renderCollapseBtn, }; }, render() { @@ -75,7 +165,10 @@ const IBizQuillPreview: any = defineComponent({ this.disabled ? this.ns.m('disabled') : '', this.readonly ? this.ns.m('readonly') : '', this.ns.m(this.lang.toLowerCase()), + this.showCollapse ? this.ns.m('show-collapse') : '', + this.ns.is('collapse', this.isCollapse), ]} + style={`--default-height: ${this.collapseHeight}`} >
{this.controller.valueMode === 'html' ? ( @@ -87,6 +180,7 @@ const IBizQuillPreview: any = defineComponent({
) : null} + {this.showCollapse && this.showCollapseBtn && this.renderCollapseBtn()} { this.editing = true; }} diff --git a/src/locale/en/index.ts b/src/locale/en/index.ts index 5d62d3bd622f0e1dd8b3aa92cc6738c8d677eb84..08224cee1b2a0a916109f38a978b9bdb092e99b1 100644 --- a/src/locale/en/index.ts +++ b/src/locale/en/index.ts @@ -107,6 +107,10 @@ export default { cancel: 'Cancel', confirm: 'Confirm', }, + html: { + expand: 'Expand', + collapse: 'Collapse', + }, cascader: { ibizCascader: { title: 'Title {index}', diff --git a/src/locale/zh-CN/index.ts b/src/locale/zh-CN/index.ts index 85809685ab1b21530058b3b8df1eb48c88510b90..a75809417f93513cabd0008efa461992ccef4234 100644 --- a/src/locale/zh-CN/index.ts +++ b/src/locale/zh-CN/index.ts @@ -105,6 +105,10 @@ export default { cancel: '取消', confirm: '确定', }, + html: { + expand: '展开', + collapse: '收起', + }, cascader: { ibizCascader: { title: '标题{index}',