@@ -5,6 +5,7 @@ import clsx from "clsx";
55import { useChangeTheme } from "@/themeToggle" ;
66import { useEmbedContext } from "./embedContext" ;
77import { LangConstants } from "@my-code/runtime/languages" ;
8+ import { MinMaxButton , Modal } from "./modal" ;
89
910// https://github.com/securingsincity/react-ace/issues/27 により普通のimportができない
1011const AceEditor = lazy ( async ( ) => {
@@ -48,15 +49,30 @@ export function EditorComponent(props: EditorProps) {
4849 } , [ files , props . filename , props . initContent , writeFile ] ) ;
4950
5051 const [ fontSize , setFontSize ] = useState < number > ( ) ;
52+ const [ windowHeight , setWindowHeight ] = useState < number > ( 1000 ) ;
5153 const [ initAce , setInitAce ] = useState ( false ) ;
5254 useEffect ( ( ) => {
53- setFontSize (
54- parseFloat ( getComputedStyle ( document . documentElement ) . fontSize )
55- ) ; // 1rem
56- setInitAce ( true ) ;
55+ const update = ( ) => {
56+ setFontSize (
57+ parseFloat ( getComputedStyle ( document . documentElement ) . fontSize )
58+ ) ; // 1rem
59+ setWindowHeight ( window . innerHeight ) ;
60+ setInitAce ( true ) ;
61+ } ;
62+ update ( ) ;
63+ window . addEventListener ( "resize" , update ) ;
64+ return ( ) => window . removeEventListener ( "resize" , update ) ;
5765 } , [ ] ) ;
58- // 最小8行 or 初期内容+1行
59- const editorHeight = Math . max ( props . initContent . split ( "\n" ) . length + 1 , 8 ) ;
66+ // 現在の内容の行数、最小8行、最大50vh
67+ const editorHeight = Math . max (
68+ Math . min (
69+ code . split ( "\n" ) . length ,
70+ Math . floor ( ( windowHeight * 0.5 ) / ( ( fontSize || 16 ) + 1 ) )
71+ ) ,
72+ 8
73+ ) ;
74+
75+ const [ isModal , setIsModal ] = useState ( false ) ;
6076
6177 if (
6278 process . env . NODE_ENV === "development" &&
@@ -68,7 +84,12 @@ export function EditorComponent(props: EditorProps) {
6884 }
6985
7086 return (
71- < div className = "border border-accent border-2 shadow-md m-2 rounded-box overflow-hidden" >
87+ < Modal
88+ id = { `edit-${ props . filename } ` }
89+ className = { clsx ( "overflow-hidden" , "flex flex-col" ) }
90+ open = { isModal }
91+ setOpen = { setIsModal }
92+ >
7293 < div className = "flex flex-row items-center bg-base-200" >
7394 < span className = "mt-2 mb-1 ml-3 mr-2 text-sm text-left" >
7495 < span >
@@ -80,9 +101,7 @@ export function EditorComponent(props: EditorProps) {
80101 </ span >
81102 < button
82103 className = { clsx (
83- "btn btn-xs btn-soft btn-warning mt-1 mb-1" ,
84- // btn-warning は文字色を変えるがsvgの色は変えてくれないので、 stroke-warning を追加指定している
85- "stroke-warning hover:stroke-warning-content active:stroke-warning-content" ,
104+ "btn btn-sm btn-soft btn-warning my-1" ,
86105 // codeの内容が変更された場合のみ表示する
87106 ( props . readonly || code == props . initContent ) && "invisible"
88107 ) }
@@ -93,6 +112,7 @@ export function EditorComponent(props: EditorProps) {
93112 className = "w-3 h-3"
94113 viewBox = "0 0 24 24"
95114 fill = "none"
115+ stroke = "currentColor"
96116 xmlns = "http://www.w3.org/2000/svg"
97117 >
98118 < g id = "Edit / Undo" >
@@ -106,8 +126,10 @@ export function EditorComponent(props: EditorProps) {
106126 />
107127 </ g >
108128 </ svg >
109- 元の内容に戻す
129+ < span className = "hidden md:inline" > 元の内容に戻す</ span >
110130 </ button >
131+ < div className = "flex-1" />
132+ < MinMaxButton open = { isModal } id = { `edit-${ props . filename } ` } />
111133 </ div >
112134 { fontSize !== undefined && initAce ? (
113135 < Suspense
@@ -121,7 +143,7 @@ export function EditorComponent(props: EditorProps) {
121143 theme = { theme }
122144 tabSize = { props . language . tabSize ?? 4 }
123145 width = "100%"
124- height = { editorHeight * ( fontSize + 1 ) + "px" }
146+ height = { isModal ? "100%" : editorHeight * ( fontSize + 1 ) + "px" }
125147 className = "font-mono!" // Aceのデフォルトフォントを上書き
126148 readOnly = { props . readonly }
127149 fontSize = { fontSize }
@@ -135,26 +157,30 @@ export function EditorComponent(props: EditorProps) {
135157 />
136158 </ Suspense >
137159 ) : (
138- < FallbackPre editorHeight = { editorHeight } > { code } </ FallbackPre >
160+ < FallbackPre isModal = { isModal } editorHeight = { editorHeight } >
161+ { code }
162+ </ FallbackPre >
139163 ) }
140- </ div >
164+ </ Modal >
141165 ) ;
142166}
143167
144168function FallbackPre ( {
145169 children,
146170 editorHeight,
171+ isModal,
147172} : {
148173 children : string ;
149174 editorHeight : number ;
175+ isModal ?: boolean ;
150176} ) {
151177 // AceEditorはなぜかline-heightが小さい
152178 // fontSize + 1px になるっぽい?
153179 return (
154180 < pre
155181 className = "font-mono overflow-auto bg-base-300 px-2 cursor-wait"
156182 style = { {
157- height : `calc((1em + 1px) * ${ editorHeight } )` ,
183+ height : isModal ? "100%" : `calc((1em + 1px) * ${ editorHeight } )` ,
158184 lineHeight : "calc(1em + 1px)" ,
159185 } }
160186 >
0 commit comments