KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

コンポーネント

アクションとブラウザーイベント

ハンドラーには KScript の文を記述でき、インポートされた関数を呼び出すこともできます。ハンドラーは関数やコンポーネントのメソッドにすることもでき、その場合は呼び出されるため、@on-click($save) は fn save() を実行します。bool フィールドを反転させるには toggle: の省略記法を使います。@on-click(toggle: $open) は @on-click($open = not $open) と同じです。@on-click($open) のような単独の bool は誤りで、toggle: への修正候補が表示されます。

ハンドラーの省略記法

ハンドラーでは、文ではなく処理の名前を指定できます。各ショートハンドは省略可能で、文の形式も引き続き使えます。@on-click(add: $draft, to: $tags, set: $draft, to: '')のように、1つのハンドラーで複数のショートハンドを左から右へ実行できます。ハンドラーにはショートハンドか文のどちらかを入れ、両方を混在させることはできません。

ショートハンドー 同等の書き方 フィールド
toggle: $open open = not $open bool
call: $save コンポーネントのfn save()、または変数内の関数 fn またはメソッド
call: $save, with: [$note.id] $this->save($note.id) fn またはメソッド
increment: $count count = $count + 1 number
increment: $count, by: 5 count = $count + 5 number
decrement: $count, by: count = $count - 1 number
set: $tab, to: 'notes' tab = 'notes' any
add: $draft, to: $tags tags = $tags->push($draft) list
remove: $tag, from: $tags tags = $tags->remove($tag) list
reset: $draft, reset: [$title, $done] 各フィールドを、開始時の値(デフォルト値、または<state>の値)に戻します any
cycle: $view, through: ['list', 'grid'] リスト内の次の値へ移り、末尾から先頭に戻ります(through:がない場合は、そのフィールドの enum の次の値) any
save: $note $note->save() KScript.Data レコード
delete: $note $note->delete() KScript.Data レコード
emit: 'notes-changed' @emit_event('notes-changed') なし
go: '/notes' @live_redirect('/notes') なし

修飾子はアクションに付随し、実行のタイミングや方法を変更します。ブラウザーは何かを送信する前に修飾子を適用します。

修飾子 機能
confirm: 'Remove this note?' 人に確認を求め、同意された場合にのみ実行します。
key: enter, key: [ctrl, s] そのキーで、かつ指定した修飾キー(ctrl、alt、shift、meta)の組み合わせのときにのみ実行します。キーはenter、escape、space、tab、up、down、left、right、英字または数字です。
prevent: true ctrl+s でページを保存するなど、ブラウザー自身の動作を止めます。
debounce: 300ms イベントが指定した時間止まるまで待ってから、一度だけ実行します。
throttle: 1s 指定した時間内に最大1回実行します。
once: true 最初の1回のみ実行します。
self: true イベントが要素自身から始まった場合にのみ実行し、その内側の要素から始まった場合は実行しません。
disable: true アクションの実行中、その要素を無効にします。
HTMLページ
<input @bind($query, debounce: 300ms) @on-keydown(reset: $query, key: escape)>
<button @on-click(delete: $note, confirm: 'Delete this note?', emit: 'notes-changed')>Delete</button>
<form @on-keydown(save: $note, key: [ctrl, s], prevent: true)>…</form>

ウィンドウでは、アクションをボタンや入力欄に直接指定します: Button(toggle: $open) "Details"とButton(increment: $count, by: 5) "+5"です。修飾子にはブラウザーが必要なので、ブラウザーを持たないウィンドウのハンドラーでは、その旨のエラーで停止します。

decrement:は、min:より小さい値にはなりません。stock: int(min: 0)の場合、decrement: $stock, by: 2が1のとき、値は0のままです。

kscript checkとエディターは、各フィールドの型を確認し、名前を補完し(また、対応するアクションの後にby:、to:、from:、with:を補完し)、ショートハンドが表す文をホバー時に表示します。アクション名の後では、補完はそのアクションに合うフィールドのみを表示します。toggle:はbool フィールド、increment:は数値、to:の後のadd:はリストを表示します。

クラスの切り替え

@class(...)は、条件が真である間、タグ自身のclassにクラスを追加します。条件ごとに1つのクラスを指定する場合はクラスを先に、複数のクラスを一度に切り替える場合は条件を先に書きます:

HTMLページ
<li class="item" @class(done: $todo.done, 'is-late': $todo.due < @today())>{$todo.title}</li>
<li @class({
  $todo.done: ['done', 'struck'],
  [$todo.done, $open]: 'expanded',
  $todo.done or $open: 'highlight',
})>{$todo.title}</li>

条件は bool、またはすべて真である必要がある bool のリストです。条件を先に書く場合、値はクラス名またはそのリストです。ブラウザーの詳細は$eventとして利用できます。キーイベントにはキー、コード、修飾キーが含まれます。ポインターイベントには、該当する場合は座標、ボタン、ポインターID、ポインターの種類が含まれます。$event.xと$event.yは、ハンドラー自身の要素内でのポインターの位置を示し、その要素がスクロールされている量も含みます。そのため、スクロールするボード上の@on-dblclick(call: $add, with: [$event.x, $event.y])は、ボードのコンテンツ内の位置を返します。

操作 属性
ボタンとフォーム on-click, on-dblclick, on-submit
入力の変更 on-input, on-change
キーボードとフォーカス on-keydown, on-keyup, on-enter, on-escape, on-focusin, on-focusout
ダイアログとポインターイベント on-cancel, on-close, on-pointerdown, on-pointerup, on-pointercancel
ライフサイクル on-init、on-visible、on-tickとevery="1s"
他のコンポーネントとクライアントコード on-eventと名前付きのon-action

Use prevent: true in a handler to cancel a browser default synchronously, and confirm: 'Remove this note?' to confirm a destructive action before posting it. @show($open) keeps a tag in the page and hides it while the value is false, so focus and scroll survive, and @live-disable(while: $saving) disables a control while a condition holds as well as while an action runs. An action can call @live_redirect('/notes') for a same-site redirect or @skip_render() to save state without updating the current HTML.

コンポーネント間のイベント

アクションから@emit_event('notes-changed', data: $note.id, target: 'summary')でイベントを発行します。ターゲットを省略すると全体に送信されます。受信する各ブロックは、on-event="$latest = $event.data" event-name="notes-changed"などの最初に一致するリスナーを実行します。ハンドラーは$event.name、$event.data、$event.sourceを読み取れます。

ドラッグ、ドロップ、並べ替え

To drag one element onto another, put @drag(copy: true, value: $item.id) on the source and on-drop on the destination (see the next section). The action reads $event.drag_value, and $event.x and $event.y give the drop point inside the destination. @drop-class('over') on the destination adds the class while something it takes is dragged over it.

@drag で要素を移動する

@drag()を使うと、訪問者はポインターで絶対配置の要素を移動できます。ドラッグが終わると、要素のon-dragendハンドラーが、ドロップ先の位置を受け取って実行されるので、アクションでその位置を保存できます:

HTMLページ
<article class="note" style="left: {$note.x}px; top: {$note.y}px; z-index: {$note.z}"
  @drag(front: true, skip: '.thumb')
  @on-dragend(call: $move, with: [$event.x, $event.y, $event.z, $event.vx])>
  …
</article>
$eventフィールド 値
x, y 要素の新しいleftとtopです。配置の基準となる親要素内のピクセル単位で表されます。
vx, vy 手を離したときの移動速度です。単位はフレームあたりのピクセル数です。
moved 押下がクリックだった場合にfalseになります。クリックでもハンドラーは実行されるので、クリックで要素を前面に出すこともできます。
z With front: true, the z-index it was raised to above its siblings.
オプション 機能
front: true 同じ配置の親要素内にある他の@drag要素より要素を前面に出します。
scroll: '.surface' ポインターが端に近づくとスクロールされるコンテナーです。既定では、最も近いスクロール可能な祖先になります。
skip: '.thumb' これらの要素上での押下は、ドラッグではなくそれらの要素自身の動作になります。入力欄、ボタン、リンク、ラベルは常に対象外です。
threshold: 6 押下がドラッグとみなされるまでにポインターが移動する距離です。既定値は4ピクセルで、クリックはクリックのままになります。
copy: true 要素自体ではなく、on-dropのターゲットへドラッグされるゴースト(複製)をドラッグします。単純なクリックでは、要素のon-clickが実行されます。
value: $color With copy:, what the drop handler reads as $event.drag_value.
ghost: 'note-ghost' With copy:, the classes of the ghost. Its top centre follows the pointer. Defaults to the element's own classes.

移動中、要素(またはゴースト)にはdraggingクラスとCSS変数--drag-vx、--drag-vy、--drag-speedがあるため、スタイルシートで移動に合わせて傾けられます。ドロップ後はアニメーションが終わるまでdrag-settledが付きます。

CSS
.note.dragging {
  --tilt-y: clamp(-28deg, calc(var(--drag-vx) * 1.6deg), 28deg);
  transform: perspective(900px) rotateY(var(--tilt-y));
}
.note.drag-settled { animation: settle 0.5s ease-out; }

A tray item that drops a new note on a board uses copy::

HTMLページ
<button type="button" class="pad" @drag(copy: true, value: $color, ghost: "note-ghost c-$color") @on-click($this->add($color))></button>
<div class="surface" @on-drop(call: $add, with: [$event.drag_value, $event.x, $event.y]) @drop-class('over')>…</div>

要素にドロップされたファイルを受け取るには、@drop-files('upload')を指定し、その中にuploadという名前のlive-uploadファイル入力を配置します。ドロップされたファイルは、手で選択されたファイルと同様に、その入力とon-changeアクションを経由して処理されます:

HTMLページ
<article class="note" @drop-files('upload') @drop-class('file-over')>
  <label>Attach <input type="file" name="upload" multiple hidden live-upload @on-change($this->attach($event.files.upload))></label>
</article>

kscript checkは、要素内に一致するlive-upload入力がない@drop-files名を報告します。

並べ替え可能な兄弟要素には、各項目に一意のsort-item="{$item.id}"を付け、そのコンテナにon-sortを指定してください。ハンドラーには、項目ID、移動前と移動後の0始まりのインデックス、および提案された順序が渡されます。ユーザーは項目をドラッグするか、フォーカスしてAlt+UpまたはAlt+Downを使用できます。左から右に並ぶリストの場合はsort-axis="horizontal"を設定してください。新しい順序を保存する前に、項目IDを検証してください。