選択イベントの使用

日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

次のReact例は、SELECTION_CHANGEDイベントの使用方法を示しています。

import ReactDOM from "react-dom";
import React from "react";

interface Context {
  api: {
    selection: {
      addToSelection: (
        ids: Array<number>,
        selectionPoolIdentifier: string,
        subPoolId?: number
      ) => void;
      removeFromSelection: (
        ids: Array<number>,
        selectionPoolIdentifier: string,
        subPoolId?: number
      ) => void;
      clearSelection: (
        selectionPoolIdentifier: string,
        subPoolId?: number,
        definitionNames?: Array<string>
      ) => void;
    };
  };
}

const onSelectionChanged = (evt: Event): void => {
  const { selectionPoolIdentifier, subPoolId, action, ids, definitionNames } = (
    evt as CustomEvent<{
      selectionPoolIdentifier: string;
      subPoolId?: number;
      action: "add" | "remove" | "clear";
      ids?: Array<number>;
      definitionNames: Array<string>;
    }>
  ).detail;

  alert(
    `selectionPoolIdentifier: ${selectionPoolIdentifier}, subPoolId: ${subPoolId}, action: ${action}, ids: ${ids}, definitionNames: ${definitionNames}`
  );
};

window.addEventListener("SELECTION_CHANGED", onSelectionChanged);

export default function createExternalRoot(container: HTMLElement) {
  return {
    render(context: Context) {
      const { addToSelection, removeFromSelection, clearSelection } =
        context.api.selection;
      const idsToAdd = [33379, 33380, 33378];
      const idsToRemove = [33379];
      const subPoolId = 35760;
      const selectionPoolIdentifier = "SelectionPool.Collection";
      const definitionNames = ["M.Asset"];

      ReactDOM.render(
        <>
          <button
            onClick={() => {
              addToSelection(idsToAdd, selectionPoolIdentifier, subPoolId);
            }}
          >
            Add to selection
          </button>
          <button
            onClick={() => {
              removeFromSelection(
                idsToRemove,
                selectionPoolIdentifier,
                subPoolId
              );
            }}
          >
            Remove from selection
          </button>
          <button
            onClick={() => {
              clearSelection(
                selectionPoolIdentifier,
                subPoolId,
                definitionNames
              );
            }}
          >
            Clear selection
          </button>
        </>,
        container
      );
    },
    unmount() {
      window.removeEventListener("SELECTION_CHANGED", onSelectionChanged);
      ReactDOM.unmountComponentAtNode(container);
    },
  };
}
この記事を改善するための提案がある場合は、 お知らせください!