@@ -8,8 +8,9 @@ import { getEmojisWidthHeight } from './emoji';
88import { Element } from 'slate' ;
99import { getStrokeWidthByElement } from '../node-style/shape' ;
1010import { getDefaultMindElementFontSize } from '../mind' ;
11- import { DEFAULT_FONT_SIZE , MarkTypes , PlaitMarkEditor } from '@plait/text-plugins' ;
12- import { getFirstTextEditor } from '@plait/common' ;
11+ import { DEFAULT_FONT_SIZE , getFirstTextMarks , MarkTypes , PlaitMarkEditor } from '@plait/text-plugins' ;
12+ import { DEFAULT_FONT_FAMILY , getElementSize , getFirstTextEditor } from '@plait/common' ;
13+ import { NodeTopicThreshold } from '../../constants/node-topic-style' ;
1314
1415const NodeDefaultSpace = {
1516 horizontal : {
@@ -61,24 +62,55 @@ export const NodeSpace = {
6162 NodeSpace . getEmojiLeftSpace ( board , element ) +
6263 getEmojisWidthHeight ( board , element ) . width +
6364 getSpaceEmojiAndText ( element ) +
64- NodeSpace . getNodeDynamicWidth ( board , element ) +
65+ NodeSpace . getTopicDynamicWidth ( board , element ) +
6566 nodeAndText
6667 ) ;
6768 }
68- return nodeAndText + NodeSpace . getNodeDynamicWidth ( board , element ) + nodeAndText ;
69+ return nodeAndText + NodeSpace . getTopicDynamicWidth ( board , element ) + nodeAndText ;
6970 } ,
7071 getNodeHeight ( board : PlaitMindBoard , element : MindElement ) {
72+ const topicSize = getElementSize (
73+ board ,
74+ element . data . topic ,
75+ { fontSize : DEFAULT_FONT_SIZE , fontFamily : DEFAULT_FONT_FAMILY } ,
76+ NodeSpace . getTopicMaxDynamicWidth ( board , element )
77+ ) ;
78+ const normalizedSize = normalizeWidthAndHeight ( board , element , topicSize . width , topicSize . height ) ;
7179 const nodeAndText = getVerticalSpaceBetweenNodeAndText ( board , element ) ;
7280 if ( MindElement . hasImage ( element ) ) {
73- return NodeSpace . getTextTopSpace ( board , element ) + element . height + nodeAndText ;
81+ return NodeSpace . getTextTopSpace ( board , element ) + normalizedSize . height + nodeAndText ;
7482 }
75- return nodeAndText + element . height + nodeAndText ;
83+ return nodeAndText + normalizedSize . height + nodeAndText ;
7684 } ,
77- getNodeDynamicWidth ( board : PlaitMindBoard , element : MindElement ) {
78- const width = element . manualWidth || element . width ;
85+ getTopicDynamicWidth ( board : PlaitMindBoard , element : MindElement ) {
86+ const topicSize = getElementSize (
87+ board ,
88+ element . data . topic ,
89+ { fontSize : getDefaultMindElementFontSize ( board , element ) , fontFamily : DEFAULT_FONT_FAMILY } ,
90+ NodeSpace . getTopicMaxDynamicWidth ( board , element )
91+ ) ;
92+ const normalizedSize = normalizeWidthAndHeight ( board , element , topicSize . width , topicSize . width ) ;
93+ const width = element . manualWidth || normalizedSize . width ;
7994 const imageWidth = MindElement . hasImage ( element ) ? element . data . image ?. width : 0 ;
8095 return Math . max ( width , imageWidth ) ;
8196 } ,
97+ getTopicHeight ( board : PlaitMindBoard , element : MindElement ) {
98+ const topicSize = getElementSize (
99+ board ,
100+ element . data . topic ,
101+ { fontSize : DEFAULT_FONT_SIZE , fontFamily : DEFAULT_FONT_FAMILY } ,
102+ NodeSpace . getTopicMaxDynamicWidth ( board , element )
103+ ) ;
104+ const normalizedSize = normalizeWidthAndHeight ( board , element , topicSize . width , topicSize . height ) ;
105+ return normalizedSize . height ;
106+ } ,
107+ getTopicMaxDynamicWidth ( board : PlaitMindBoard , element : MindElement ) {
108+ return Math . max (
109+ NodeTopicThreshold . defaultTextMaxWidth ,
110+ element . manualWidth || 0 ,
111+ MindElement . hasImage ( element ) ? element . data . image ?. width : 0
112+ ) ;
113+ } ,
82114 /**
83115 * use it when upload image first or resize image
84116 */
@@ -96,9 +128,8 @@ export const NodeSpace = {
96128 } ,
97129 getNodeTopicMinWidth ( board : PlaitMindBoard , element : MindElement ) {
98130 const defaultFontSize = getDefaultMindElementFontSize ( board , element ) ;
99- const editor = getFirstTextEditor ( element ) ;
100- const marks = PlaitMarkEditor . getMarks ( editor ) ;
101- const fontSize = ( marks [ MarkTypes . fontSize ] as number ) || defaultFontSize ;
131+ const firstText = getFirstTextMarks ( element . data . topic ) ;
132+ const fontSize = ( firstText [ MarkTypes . fontSize ] ? Number ( firstText [ MarkTypes . fontSize ] ) : null ) || defaultFontSize ;
102133 return fontSize ;
103134 } ,
104135 getTextLeftSpace ( board : PlaitMindBoard , element : MindElement ) {
@@ -141,3 +172,9 @@ export const getFontSizeBySlateElement = (text: string | Element) => {
141172 const fontSize = ( marks [ MarkTypes . fontSize ] as number ) || defaultFontSize ;
142173 return fontSize ;
143174} ;
175+
176+ export const normalizeWidthAndHeight = ( board : PlaitMindBoard , element : MindElement , width : number , height : number ) => {
177+ const minWidth = NodeSpace . getNodeTopicMinWidth ( board , element ) ;
178+ const newWidth = width < minWidth ? minWidth : width ;
179+ return { width : newWidth , height } ;
180+ } ;
0 commit comments