문서

텍스트 정렬

문단과 목록 항목의 정렬을 바꿉니다.

직접 확인하기

“지금은 툴바가 발표할 차례입니다.” 안에 커서를 놓고 가운데를 누르세요. 문단의 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을 등록하세요.

기능