ドキュメント

テキストの配置

段落やリスト項目の配置を変えます。

試してみる

例の段落内で 中央揃え を押すと HTML に style="text-align: center" が付きます。JSON の attrs.textAlign は "center" です。

実行例
適用結果

エディターに追加する

TextAlign を有効にし、左、中央、右、両端揃えをコマンドに接続します。

import {
  EditorContent, EditorProvider, TextAlign, useEditor, useIsActive,
  type EditorInstance,
} from 'yurich';

const choices = [
  ['alignLeft', "左揃え"],
  ['alignCenter', "中央揃え"],
  ['alignRight', "右揃え"],
  ['alignJustify', "両端揃え"],
] as const;

function AlignButton({ editor, command, label }: {
  editor: EditorInstance;
  command: (typeof choices)[number][0];
  label: string;
}) {
  const active = useIsActive(editor, command);

  return (
    <button
      type="button"
      aria-pressed={active}
      onMouseDown={event => event.preventDefault()}
      onClick={() => editor.execute(command)}
    >
      {label}
    </button>
  );
}

export function AlignmentEditor() {
  const editor = useEditor({
    extensions: [TextAlign],
    initialValue: "<p>ここからはツールバーの出番です。</p>",
  });

  return (
    <EditorProvider editor={editor}>
      <div role="group" aria-label="文字揃え">
        {choices.map(([command, label]) => (
          <AlignButton key={command} editor={editor} command={command} label={label} />
        ))}
      </div>
      <EditorContent ariaLabel="エディター" />
    </EditorProvider>
  );
}

動作の補足

複数のブロックに適用されます。保存した配置を保つには読み込み先でも TextAlign を有効にします。

機能