인라인 서식 만들기
mark()로 문장 일부에 적용할 서식을 만듭니다. Yurich를 선택하고 강조 표시를 누르면 그 단어에 <mark>가 적용됩니다.
실행 예제
적용 결과
import { mark, EditorContent, EditorProvider, useEditor, useIsActive } from 'yurich';
const Highlight = mark({ name: 'highlight', tag: 'mark', shortcut: 'Mod+Shift+H' });
export function HighlightEditor() {
const editor = useEditor({
extensions: [Highlight],
initialValue: "<p>오늘은 Yurich를 조금 강조해 봅시다.</p>",
});
const active = useIsActive(editor, 'toggleHighlight');
return (
<EditorProvider editor={editor}>
<button
type="button"
aria-pressed={active}
onMouseDown={event => event.preventDefault()}
onClick={() => editor.execute('toggleHighlight')}
>
강조 표시
</button>
<EditorContent ariaLabel="편집기" />
</EditorProvider>
);
}
결과 확인
editor.getHTML()로 읽으면 Yurich만 <mark> 태그로 감싸져 있습니다.
<p>오늘은 <mark>Yurich</mark>를 조금 강조해 봅시다.</p>
editor.getJSON()은 문서 전체를 트리로 반환합니다. 아래는 Yurich에 해당하는 텍스트 노드입니다. marks의 highlight는 mark()에 등록한 서식 이름입니다.
{
"type": "text",
"text": "Yurich",
"marks": [{ "type": "highlight" }]
}
태그 이름은 소문자로 씁니다. mark()의 aliases로 뜻이 같은 입력 태그를 받아들일 수 있습니다.