Schema Definition
Schema definition for the Font Size mark type:
{
fontSize: {
attrs: {
size: { default: 16 }
},
style: 'font-size: ' + node.attrs.size + 'px'
}, 0]
}
}Model Representation
Example model representation:
{
type: 'text',
text: 'Large text',
marks: [{
type: 'fontSize',
attrs: { size: 24 }
}]
}HTML Serialization
Converting model to HTML:
function serializeFontSizeMark(text, mark) {
const size = mark.attrs?.size || 16;
return '<span style="font-size: ' + size + 'px">' + text + '</span>';
}HTML Deserialization
Parsing HTML to model:
function extractFontSizeMark(element) {
if (element.tagName === 'SPAN' && element.style.fontSize) {
const size = parseInt(element.style.fontSize) || 16;
return {
type: 'fontSize',
attrs: { size }
};
}
return null;
}View Integration
View Integration Notes: Pay special attention to contenteditable behavior, selection handling, and event management when implementing this node type in your view layer.
View integration code:
// Setting font size
function setFontSize(size) {
addMark({
type: 'fontSize',
attrs: { size }
});
}
// Font size picker
function showFontSizePicker() {
const sizes = [12, 14, 16, 18, 20, 24, 28, 32];
// Show size selector
}Common Issues
Common Pitfalls: These are issues frequently encountered when implementing this node type. Review carefully before implementation.
Common issues and solutions:
// Issue: Font size units (px, em, rem)
// Solution: Normalize to px
function normalizeFontSize(size) {
if (typeof size === 'string') {
if (size.endsWith('px')) {
return parseInt(size);
}
if (size.endsWith('em')) {
return parseFloat(size) * 16; // Assume 16px base
}
}
return size;
}
// Issue: Font size validation
// Solution: Clamp to valid range
function validateFontSize(size) {
return Math.max(8, Math.min(72, size));
}Implementation
Complete implementation example:
class FontSizeMark {
constructor(attrs) {
this.type = 'fontSize';
this.attrs = { size: attrs?.size || 16 };
}
toDOM() {
return ['span', {
style: 'font-size: ' + this.attrs.size + 'px'
}, 0];
}
static fromDOM(element) {
if (element.tagName === 'SPAN' && element.style.fontSize) {
return new FontSizeMark({
size: parseInt(element.style.fontSize) || 16
});
}
return null;
}
}