From a5fd73f5fcecf9e2667fc9946fff397082bbdc10 Mon Sep 17 00:00:00 2001 From: leecason Date: Sun, 26 Jul 2020 17:47:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20fix=20initial=20lineHeigh?= =?UTF-8?q?t=20wrong=20transform?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close https://github.com/Leecason/element-tiptap/issues/148 --- src/extensions/paragraph.ts | 6 +++--- src/utils/line_height.ts | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/extensions/paragraph.ts b/src/extensions/paragraph.ts index bb23c39d..ae9190c7 100644 --- a/src/extensions/paragraph.ts +++ b/src/extensions/paragraph.ts @@ -1,7 +1,7 @@ import { NodeSpec, Node as ProsemirrorNode, DOMOutputSpec } from 'prosemirror-model'; import { Paragraph as TiptapParagraph } from 'tiptap'; -import { transformLineHeightToCSS } from '@/utils/line_height'; -import { ALIGN_PATTERN, LINE_HEIGHT_100 } from '@/constants'; +import { transformLineHeightToCSS, transformCSStoLineHeight } from '@/utils/line_height'; +import { ALIGN_PATTERN } from '@/constants'; export const ParagraphNodeSpec: NodeSpec = { attrs: { @@ -30,7 +30,7 @@ function getAttrs (dom): { [key: string]: any } { const indent = parseInt(dom.getAttribute('data-indent'), 10) || 0; - lineHeight = (lineHeight && lineHeight !== transformLineHeightToCSS(LINE_HEIGHT_100)) ? lineHeight : null; + lineHeight = transformCSStoLineHeight(lineHeight) || null; return { textAlign: align, diff --git a/src/utils/line_height.ts b/src/utils/line_height.ts index 7b7037c3..28d2cf8e 100644 --- a/src/utils/line_height.ts +++ b/src/utils/line_height.ts @@ -48,7 +48,22 @@ export function transformLineHeightToCSS (value: string | number): string { strValue = String(Math.round(numValue * 100)) + '%'; } - return parseFloat(strValue) * LINE_HEIGHT_100 + '%'; ; + return parseFloat(strValue) * LINE_HEIGHT_100 + '%'; +} + +export function transformCSStoLineHeight (value: string): string { + if (!value) return ''; + if (value === DEFAULT_LINE_HEIGHT) return ''; + + let strValue = value; + + if (NUMBER_VALUE_PATTERN.test(value)) { + const numValue = parseFloat(value); + strValue = String(Math.round(numValue * 100)) + '%'; + if (strValue === DEFAULT_LINE_HEIGHT) return ''; + } + + return parseFloat(strValue) / LINE_HEIGHT_100 + '%'; } interface SetLineHeightTask {