윈도우와 터미널의 구성 요소
컴포넌트는 페이지에 묶여 있지 않아요. 컴포넌트가 그려질 수 있는 곳마다 각각 render가 있으며, 각 곳의 내용을 보면 어떤 것인지 알 수 있어요.
render가 보관하는 것 |
그려지는 곳 |
|---|---|
html 블록이 반환됨 |
페이지 |
KScript.UI 요소(예: Column, Text, Button) |
창 |
echo 또는 KScript.Terminal 요소 |
터미널 |
KScript.Document 요소(예: Section, Paragraph)나 반환된 md 또는 latex 블록 |
문서 |
컴포넌트마다 각 위치에 최대 하나의 render만 있을 수 있어요. 필드, construct 블록, 메서드는 모든 위치에서 공유돼요.
samples/components에는 네 가지가 모두 담긴 레시피 카드가 하나 있어요: 페이지, 데스크톱 창, 터미널, 문서.
<import package="KScript.UI" />
<type name="NoteCard" extends="Component">
<construct>
<field name="title" type="string" />
<field name="body" type="string" default="''" />
</construct>
<field name="open" type="bool" />
<fn name="render">
<Box padding="8" border="#334155">
<Row gap="8">
<Text weight="bold">{.title}</Text>
<Button toggle="$open">{ .open ? 'Hide' : 'Show' }</Button>
</Row>
<if .open>
<Text wrap="yes">{.body}</Text>
<slot />
</if>
</Box>
</fn>
<fn name="render" surface="terminal">
<echo>* {.title}: {.body}</echo>
<slot />
</fn>
</type>import KScript.UI
type NoteCard extends Component {
construct({ title: string, body: string = '' })
open: bool
fn render() {
Box(padding: 8, border: '#334155') {
Row(gap: 8) {
Text(weight: bold) "{.title}"
Button(toggle: $open) "{ .open ? 'Hide' : 'Show' }"
}
if .open {
Text(wrap) "{.body}"
slot
}
}
}
fn render(terminal) {
echo "* {.title}: {.body}"
slot
}
}스크립트에서 컴포넌트는 문장으로 작성해서 그립니다. 입력값은 인수로 넘기고요. 먼저 컴포넌트 파일을 가져오세요.
<KScript.Window title="Notes" width="420" height="320">
<import path="components/note-card.kscript" />
<let name="notes">[
{ title: 'Milk', body: 'Two pints' },
{ title: 'Eggs', body: 'Six, free range' },
]</let>
<for each="note" in="$notes">
<NoteCard title="{$note.title}" body="{$note.body}" id="{$note.title}">
<Text color="#94a3b8">From the shopping list</Text>
</NoteCard>
</for>
</KScript.Window>import KScript.UI
import "components/note-card.kscript"
Window(title: 'Notes', width: 420, height: 320) {
$notes = [
{ title: 'Milk', body: 'Two pints' },
{ title: 'Eggs', body: 'Six, free range' },
]
for note in $notes {
NoteCard(title: $note.title, body: $note.body, id: $note.title) {
Text(color: '#94a3b8') "From the shopping list"
}
}
}Window안에서는 컴포넌트가 자신의 창render로 그려요. 스크립트에서 단독으로 작성하면 자신의 터미널render로 그려지고요.kscript check는render가 없는 곳에 그려진 컴포넌트를 알려줘요.render안의slot은 호출자가 컴포넌트 안에 작성한 내용을 그려요.- 창에서는 각 컴포넌트가 그릴 때마다 자신의 상태를 유지해요. 그래서 위의
$open은 하나의 카드에 속해요. 호출자가 전달한 값은 매번 호출자를 따라가고, 나머지 필드는 컴포넌트가 설정한 대로 그대로 남아요. - 반복문에서 그려지는 컴포넌트는 순서로 구분해요. 목록의 순서가 바뀌거나 항목이 사라질 때 컴포넌트의 상태를 해당 항목과 함께 유지하려면
id:을 전달하세요. - 더 이상 그려지지 않는 컴포넌트는 사라져요. 다시 그려지면 처음부터 새로 시작해요.
render에if,for,slot컴포넌트만 들어 있으면, 내용만으로는 어디에 그려질지 알 수 없어요. 위치를 지정하세요:fn render(window),fn render(terminal)또는fn render(document).
XML에서 위치는 surface 속성(<fn name="render" surface="terminal">)으로 지정하고, 컴포넌트는 타입 이름을 딴 요소(<NoteCard title="Milk" />)로 작성해요.
편집기는 명령문을 입력하는 위치에서 사용할 수 있는 구성 요소를 입력값까지 채워서 제안해요. 대괄호 안의 입력값도 자동으로 완성해요. 각 화면 고유의 요소는 해당 위치에서만 제안해요. 예를 들어 창의 요소는 render 창 안에서만, 문서의 요소는 문서 안에서만 제안해요. fn render( 다음에는 window, terminal, document를 제안해요.
kscript make component NoteCard --for window은 fn render(window)가 있는 컴포넌트를 창 옆의 components/에 작성해요. --for document는 하나를 documents/components/에 작성하고, --for terminal는 하나를 scripts/components/에 작성해요.