調査アンケート: Blockly のご利用体験についてお聞かせください
アンケートを開始
イベント
ワークスペースの変更ごとにイベントがトリガーされます。これらのイベントは、各変更の前後の状態を完全に記述します。
イベントのリッスン
ワークスペースには、イベント ストリームのリッスンに使用できる addChangeListener
メソッドと removeChangeListener
メソッドがあります。その一例が、コードのリアルタイム生成です。もう一つの例は、最大ブロック上限のデモです。多くの場合、これらの 2 つの例のどちらも、トリガー イベントが何であるかは気にしません。ワークスペースの現在の状態を確認するだけです。
より高度なイベント リスナーは、トリガーとなるイベントを確認します。次の例では、ユーザーが最初のコメントを作成したときに検出してアラートを発行し、その後はリッスンを停止して、アラートがトリガーされないようにします。
function onFirstComment(event) {
if (event.type == Blockly.Events.BLOCK_CHANGE &&
event.element == 'comment' &&
!event.oldValue && event.newValue) {
alert('Congratulations on creating your first comment!')
workspace.removeChangeListener(onFirstComment);
}
}
workspace.addChangeListener(onFirstComment);
フライアウト内で発生したイベントをリッスンするために、フライアウトのワークスペースにリスナーを追加できます。
var flyoutWorkspace = yourWorkspace.getFlyout().getWorkspace();
flyoutWorkspace.addChangeListener(onFirstComment);
ブロックには、イベント ストリームをリッスンする別の方法があります。ブロックでは、onchange
関数を定義するか、setOnChange を使用して、ブロックのワークスペースで変更が発生するたびに呼び出される関数を設定できます。
イベントタイプ
個々のイベントについては、リファレンス ドキュメントをご覧ください。
デモ
イベントでできることの例については、ミラーのデモをご覧ください。このデモには 2 つの Blockly ワークスペースがあり、イベントを使用して同期されています。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2024-11-08 UTC。
[null,null,["最終更新日 2024-11-08 UTC。"],[[["Every workspace change triggers an event detailing the before and after state."],["Workspaces offer methods (`addChangeListener`, `removeChangeListener`) to listen for these events."],["Events can be used for various purposes like real-time code generation or triggering actions based on specific changes (e.g., a new comment)."],["Blocks can also have individual change listeners using the `onchange` function or `setOnChange`."],["Comprehensive event details are available in the Blockly reference documentation."]]],["Workspace changes trigger events, describing the before and after states. Listeners can be added using `addChangeListener` and removed with `removeChangeListener`. Listeners can react to specific events, like the creation of a comment, or monitor all events. Flyout events can be monitored by adding a listener to the flyout's workspace. Blocks can use an `onchange` function to listen for changes in their workspace. There are several event types as described in the documentation.\n"]]