The Marks namespace provides methods for text formatting operations. Marks are Slate text formatting properties like bold, italic, underline, color, and custom formatting.
import { Marks } from '@yoopta/editor';// Use with editor instanceMarks.toggle(editor, { type: 'bold' });Marks.update(editor, { type: 'color', value: '#ff0000' });// Check if mark is activeconst isBold = Marks.isActive(editor, { type: 'bold' });
// Check if bold is active in current selectionconst isBold = Marks.isActive(editor, { type: 'bold' });// Check if italic is active in specific blockconst isItalic = Marks.isActive(editor, { type: 'italic', at: 0});// Check by block IDconst hasColor = Marks.isActive(editor, { type: 'color', blockId: 'block-123'});// Use in UI to show active statefunction BoldButton() { const isBold = Marks.isActive(editor, { type: 'bold' }); return ( <button className={isBold ? 'active' : ''} onClick={() => Marks.toggle(editor, { type: 'bold' })} > Bold </button> );}
// Get color value from current selectionconst color = Marks.getValue(editor, { type: 'color' });console.log(color); // '#ff0000' or null// Get font size from specific blockconst fontSize = Marks.getValue(editor, { type: 'fontSize', at: 0});console.log(fontSize); // 18 or null// Get highlight valueconst highlight = Marks.getValue(editor, { type: 'highlight' });console.log(highlight); // { color: 'yellow', backgroundColor: '#ffff00' } or null// Use in color pickerfunction ColorPicker() { const currentColor = Marks.getValue(editor, { type: 'color' }) as string; return ( <input type="color" value={currentColor || '#000000'} onChange={(e) => Marks.update(editor, { type: 'color', value: e.target.value })} /> );}
// Get all marks from current selectionconst marks = Marks.getAll(editor);console.log(marks);// {// bold: true,// italic: true,// color: '#ff0000',// fontSize: 16// }// Get all marks from specific blockconst marks = Marks.getAll(editor, { at: 0 });
// Clear all marks from current selectionMarks.clear(editor);// Clear marks from specific blockMarks.clear(editor, { at: 0 });// Clear marks from multiple blocksMarks.clear(editor, { at: [0, 1, 2] });// Clear marks from selectionMarks.clear(editor, { selection: { anchor: { path: [0, 0], offset: 0 }, focus: { path: [0, 0], offset: 10 } }});