The Slate editor is a rich text editor implementation built on top of the Slate.js framework, specifically designed for editing legal documents. It provides a comprehensive set of features for document editing, including structure highlighting, XML preview, and various editing tools.
The editor is composed of several key components:
-
Main Editor Area
- Split into two columns
- Left column: Main editing area
- Right column: Toolbar and side panel
-
Toolbars
HoveringToolbar: Appears when text is selectedSideToolbar: Fixed toolbar on the left sideToolbar: Main toolbar at the top of the right column
-
Side Panel
- Shows XML preview
- Displays original document
- Provides additional editing controls
-
Document Structure
- Hierarchical document organization
- Structure highlighting
- XML representation
-
Editing Capabilities
- Rich text formatting
- Document structure manipulation
- Meta-data editing
- Navigation blocking for unsaved changes
-
User Interface
- Read-only mode support
- Responsive layout
- Custom styling
The editor is configured through the EditorConfig context:
interface EditorConfig {
highlightStructure: boolean;
// Add other configuration options
}The document structure is defined in law-document/src/slate/config/tags.ts. This file configures the hierarchical structure of legal documents and defines the relationships between different elements.
interface TagConfig {
type: string; // The type of the element
isList: boolean; // Whether the element is a list item
hasTitle?: boolean; // Whether the element has a title
hasName?: boolean; // Whether the element has a name
defaultTitle?: string; // Default title format
canHave: MetaType[]; // Allowed child element types
display?: 'list' | 'block' | 'inline'; // Display type
}The editor comes with predefined tags for legal documents:
-
Chapter (CHAPTER)
{ type: MetaType.CHAPTER, isList: true, hasTitle: true, hasName: true, defaultTitle: 'I. kafli. ', display: 'list', canHave: [MetaType.ART] }
- Represents a chapter in the document
- Can only contain articles
- Has a title and name
- Uses Roman numeral formatting
-
Article (ART)
{ type: MetaType.ART, isList: true, hasTitle: true, hasName: true, defaultTitle: '1. gr. ', display: 'list', canHave: [MetaType.SUBART, MetaType.NUMART] }
- Represents an article
- Can contain sub-articles and numbered articles
- Has a title and name
- Uses numeric formatting
-
Sub-Article (SUBART)
{ type: MetaType.SUBART, isList: true, display: 'list', canHave: [MetaType.PARAGRAPH] }
- Represents a sub-article
- Can only contain paragraphs
- No title or name
-
Numbered Article (NUMART)
{ type: MetaType.NUMART, isList: true, display: 'list', canHave: [MetaType.PARAGRAPH, MetaType.SEN, MetaType.NUMART] }
- Represents a numbered article
- Can contain paragraphs, sentences, and nested numbered articles
- No title or name
-
Paragraph (PARAGRAPH)
{ type: MetaType.PARAGRAPH, isList: true, display: 'list', canHave: [MetaType.SEN, MetaType.NUMART] }
- Represents a paragraph
- Can contain sentences and numbered articles
- No title or name
-
Sentence (SEN)
{ type: MetaType.SEN, isList: false, display: 'inline', canHave: [] }
- Represents a sentence
- Cannot contain other elements
- Displayed inline
- No title or name
interface EditorProps {
slate: SlateFragment; // Current document content
originalDocument: SlateFragment; // Original document for comparison
xml: string; // XML representation
readOnly?: boolean; // Read-only mode flag
saveDocument?: (editor: LawEditor) => void; // Save callback
bill?: Bill; // Associated bill data
navigationBlocker: NavigationBlocker; // Navigation control
t: Translator; // Translation function
}The editor uses several plugins to extend its functionality:
-
handleKeyDown
- Manages keyboard shortcuts
- Handles special key combinations
-
renderElement
- Custom element rendering
- Structure-specific formatting
-
renderLeaf
- Leaf node rendering
- Text formatting
-
DocumentMetaBlock
- Meta-data handling
- Document properties
import { Editor } from 'law-document-editor';
const MyComponent = () => {
const handleSave = (editor) => {
// Handle document saving
};
return (
<Editor
slate={documentContent}
originalDocument={originalContent}
xml={xmlContent}
saveDocument={handleSave}
navigationBlocker={navigationBlocker}
t={translator}
/>
);
};The editor includes custom CSS for:
- Layout and positioning
- Structure highlighting
- Toolbar appearance
- Side panel styling
-
Document Structure
- Maintain proper hierarchy
- Use appropriate element types
- Follow XML schema
-
Performance
- Use
useMemofor editor creation - Implement proper change handling
- Manage state efficiently
- Use
-
User Experience
- Provide clear feedback
- Handle unsaved changes
- Support keyboard shortcuts
The editor includes:
- Navigation blocking for unsaved changes
- Error boundaries
- Validation
- State recovery