@@ -60,47 +60,43 @@ export const createTextarea = (app: App, textNode: Konva.Text, onUpdated: () =>
60
60
textNode . show ( ) ;
61
61
}
62
62
63
- function setTextareaWidth ( newWidth : number ) {
64
- newWidth = Math . max ( textarea . value . length * textNode . fontSize ( ) , newWidth ) ;
65
-
66
- //TODO: The platform detection should be a generic function.
67
- const isSafari = / ^ ( (? ! c h r o m e | a n d r o i d ) .) * s a f a r i / i. test ( navigator . userAgent ) ;
68
- const isFirefox = navigator . userAgent . toLowerCase ( ) . indexOf ( 'firefox' ) > - 1 ;
69
- if ( isSafari || isFirefox ) {
70
- newWidth = Math . ceil ( newWidth ) ;
71
- }
72
-
73
- const isEdge = document . DOCUMENT_NODE || / E d g e / . test ( navigator . userAgent ) ;
74
- if ( isEdge ) {
75
- newWidth += 1 ;
76
- }
77
- textarea . style . width = newWidth + 'px' ;
78
- }
79
-
80
63
textarea . addEventListener ( 'keydown' , function ( e : KeyboardEvent ) {
81
64
e . stopPropagation ( ) ;
82
- if ( e . key === 'Enter' && ! e . shiftKey ) {
83
- if ( textarea . value . trim ( ) . length >= 1 ) {
84
- textNode . text ( textarea . value ) ;
85
- } else {
86
- app . remove ( textNode ) ;
87
- }
88
- removeTextarea ( ) ;
89
- onUpdated ( ) ;
90
- }
91
65
if ( e . key === 'Escape' ) {
92
66
removeTextarea ( ) ;
93
67
}
94
- const scale = textNode . getAbsoluteScale ( ) . x ;
95
- setTextareaWidth ( textNode . width ( ) * scale - textNode . padding ( ) * 2 + 20 ) ;
96
- textarea . style . height = 'auto' ;
97
- textarea . style . height = textarea . scrollHeight + textNode . fontSize ( ) + 'px' ;
68
+ } ) ;
69
+
70
+ textarea . addEventListener ( 'input' , ( ) => {
71
+ let text = textarea . value . replace ( / \n / g, '<br/>' ) ; // 将换行符替换为<br/>标签,以便正确测量宽度
72
+ let tempDiv = document . createElement ( 'div' ) ;
73
+ tempDiv . innerHTML = text ;
74
+ tempDiv . style . position = 'absolute' ;
75
+ tempDiv . style . visibility = 'hidden' ;
76
+ tempDiv . style . whiteSpace = 'pre-wrap' ; // 保持文本的换行
77
+ document . body . appendChild ( tempDiv ) ;
78
+ const newWidth = tempDiv . offsetWidth + 10 ;
79
+ textarea . style . width = newWidth * textNode . scaleX ( ) + 'px' ; // 设置宽度为文本实际宽度
80
+ document . body . removeChild ( tempDiv ) ;
81
+
82
+ let lineHeight = parseFloat ( getComputedStyle ( textarea ) . lineHeight ) ;
83
+ let rows = textarea . value . split ( '\n' ) . length ;
84
+ textarea . style . height = 'auto' ; // 重置高度以便重新计算
85
+ const newHeight = lineHeight * rows ;
86
+ textarea . style . height = newHeight + 'px' ;
87
+ textNode . width ( newWidth ) ;
88
+ textNode . height ( newHeight / textNode . scaleY ( ) ) ;
98
89
} ) ;
99
90
100
91
function handleOutsideClick ( e : MouseEvent ) {
101
92
if ( e . target !== textarea ) {
102
- textNode . text ( textarea . value ) ;
93
+ if ( textarea . value . trim ( ) . length >= 1 ) {
94
+ textNode . text ( textarea . value ) ;
95
+ } else {
96
+ app . remove ( textNode ) ;
97
+ }
103
98
removeTextarea ( ) ;
99
+ onUpdated ( ) ;
104
100
}
105
101
}
106
102
setTimeout ( ( ) => {
0 commit comments