Actions System
The actions system provides zero-dependency formatting helpers that you can use anywhere in your code.
Overview
All actions are bundled with Marzipan and accept a textarea element:
ts
import { actions } from '@pinkpixel/marzipan';
const textarea = document.querySelector('textarea');
actions.toggleBold(textarea);
Text Formatting
Bold, Italic, Code
ts
actions.toggleBold(textarea); // **bold**
actions.toggleItalic(textarea); // *italic*
actions.toggleCode(textarea); // `code`
Headers
ts
// Insert or toggle specific header level
actions.insertHeader(textarea, 1); // # H1
actions.insertHeader(textarea, 2); // ## H2
actions.insertHeader(textarea, 3); // ### H3
// Convenience methods
actions.toggleH1(textarea);
actions.toggleH2(textarea);
actions.toggleH3(textarea);
Lists
ts
actions.toggleBulletList(textarea); // - bullet
actions.toggleNumberedList(textarea); // 1. numbered
actions.toggleTaskList(textarea); // - [ ] task
Blocks
ts
actions.toggleQuote(textarea); // > quote
Links
ts
// Basic link
actions.insertLink(textarea);
// Link with URL
actions.insertLink(textarea, {
url: 'https://example.com'
});
// Link with text and URL
actions.insertLink(textarea, {
text: 'Click here',
url: 'https://example.com'
});
Utilities
ts
// Get currently active formats
const formats = actions.getActiveFormats(textarea);
// Returns array like: ['bold', 'italic']
// Check if specific format is active
const isBold = actions.hasFormat(textarea, 'bold');
// Expand selection to nearest boundaries
actions.expandSelection(textarea);
// Preserve selection during operations
actions.preserveSelection(textarea, () => {
// Do something that modifies content
});
Debug Mode
Enable debug logging for development:
ts
actions.setDebugMode(true);
// Now all action calls will log details
actions.toggleBold(textarea);
// Logs: selection info, transformation details, etc.
actions.setDebugMode(false);
Custom Formats
Create custom formatting rules:
ts
actions.applyCustomFormat(textarea, {
prefix: '~~',
suffix: '~~',
placeholder: 'strikethrough'
});
Integration Examples
Custom Toolbar
ts
const toolbar = document.createElement('div');
const boldBtn = document.createElement('button');
boldBtn.textContent = 'Bold';
boldBtn.onclick = () => actions.toggleBold(textarea);
const italicBtn = document.createElement('button');
italicBtn.textContent = 'Italic';
italicBtn.onclick = () => actions.toggleItalic(textarea);
toolbar.append(boldBtn, italicBtn);
Keyboard Shortcuts
ts
textarea.addEventListener('keydown', (e) => {
if (e.ctrlKey || e.metaKey) {
if (e.key === 'b') {
e.preventDefault();
actions.toggleBold(textarea);
}
if (e.key === 'i') {
e.preventDefault();
actions.toggleItalic(textarea);
}
}
});
Full Action Reference
For complete type definitions and advanced usage, see the Actions API Reference.
Marzipan