Skip to content

Commit

Permalink
fix: 馃悰 fix initial lineHeight wrong transform
Browse files Browse the repository at this point in the history
close #148
  • Loading branch information
Leecason committed Jul 26, 2020
1 parent fa03494 commit a5fd73f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions 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: {
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 16 additions & 1 deletion src/utils/line_height.ts
Expand Up @@ -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 {
Expand Down

0 comments on commit a5fd73f

Please sign in to comment.