Complete reference for all public methods of the SquibView class.
Sets the editor content and optionally changes the content type.
setContent(content, contentType, saveRevision = true)
Parameters:
content
(string) - The content to setcontentType
(string) - Optional. Content type (‘md’, ‘html’, ‘reveal’, ‘csv’, ‘tsv’)saveRevision
(boolean) - Whether to save this change to revision historyReturns:void
Example:
editor.setContent('# New Content', 'md');
editor.setContent('<h1>HTML Content</h1>', 'html', false);
Returns the current source content.
getContent()
Returns:string
- The current source content
Example:
const content = editor.getContent();
console.log(content);
Returns the rendered HTML output.
getHTMLSource()
Returns:string
- The rendered HTML
Example:
const html = editor.getHTMLSource();
document.getElementById('preview').innerHTML = html;
Clears all content from the editor.
clearContent()
Returns:void
Example:
editor.clearContent();
Changes the editor view mode.
setView(view)
Parameters:
view
(string) - View mode: ‘src’, ‘html’, or ‘split’Returns:void
Example:
editor.setView('split');
editor.setView('html');
Cycles through the available view modes (src → html → split → src).
toggleView()
Returns:void
Example:
editor.toggleView();
Shows only the source editor view.
showSource()
Returns:void
Example:
editor.showSource();
Shows only the rendered HTML view.
showHTML()
Returns:void
Example:
editor.showHTML();
Shows the split view with both source and rendered output.
showSplit()
Returns:void
Example:
editor.showSplit();
Changes the content type and re-renders the content.
setContentType(type)
Parameters:
type
(string) - Content type: ‘md’, ‘html’, ‘reveal’, ‘csv’, or ‘tsv’Returns:void
Example:
editor.setContentType('md');
editor.setContentType('csv');
Returns the current content type.
getContentType()
Returns:string
- Current content type
Example:
const type = editor.getContentType();
console.log(type); // 'md'
Undoes the last change and restores previous content.
revisionUndo()
Returns:boolean
- True if undo was performed, false if no history
Example:
if (editor.revisionUndo()) {
console.log('Undo performed');
}
Redoes a previously undone change.
revisionRedo()
Returns:boolean
- True if redo was performed, false if no redo history
Example:
if (editor.revisionRedo()) {
console.log('Redo performed');
}
Gets the current position in the revision history.
revisionGetCurrentIndex()
Returns:number
- Current revision index (0-based)
Example:
const currentIndex = editor.revisionGetCurrentIndex();
const totalRevisions = editor.revisionNumRevsions();
// Check if undo/redo are availableconst canUndo = currentIndex > 0;
const canRedo = totalRevisions > 0 && currentIndex < totalRevisions - 1;
if (canUndo) {
document.getElementById('undoBtn').disabled = false;
}
if (canRedo) {
document.getElementById('redoBtn').disabled = false;
}
Returns the number of revisions in history.
revisionNumRevsions()
Returns:number
- Number of saved revisions
Example:
const count = editor.revisionNumRevsions();
console.log(`${count} revisions saved`);
Jumps to a specific revision by index.
revisionSet(index)
Parameters:
index
(number) - The revision index to jump toReturns:boolean
- True if revision was set successfully
Example:
editor.revisionSet(0); // Jump to first revision
editor.revisionSet(editor.revisionNumRevsions() - 1); // Jump to latest
Copies the source content (markdown) to the clipboard.
copySource()
Returns:Promise<boolean>
- True if copy succeeded
Example:
await editor.copySource();
Copies the rendered HTML to the clipboard.
copyHTML()
Returns:Promise<boolean>
- True if copy succeeded
Example:
await editor.copyHTML();
Low-level method to copy any text string to the clipboard.
copyToClipboard(text)
Parameters:
text
(string) - The text to copyReturns:boolean
- True if copy succeeded
Example:
const success = editor.copyToClipboard('Custom text');
SquibView doesn’t expose a public method for getting selected text directly. Use the browser’s selection API:
// Get selected text from browserconst selection = window.getSelection();
const selectedText = selection ? selection.toString() : '';
// Or listen for selection events
editor.events.on('text:selected', (data) => {
console.log('Selected text:', data.text);
console.log('Source:', data.source); // 'input' or 'output'
});
Returns detailed information about the current selection.
getSelectionData()
Returns:Object
- Selection data with properties:
text
(string) - Selected texthtml
(string) - Selected HTML (if from rendered view)markdown
(string) - Converted markdown (if applicable)start
(number) - Start position (source view)end
(number) - End position (source view)source
(string) - ‘input’ or ‘output’Example:
const selection = editor.getSelectionData();
console.log('Selected text:', selection.text);
console.log('From:', selection.source);
Sets a handler for replacing selected text.
setOnReplaceSelectedText(handler)
Parameters:
handler
(function) - Callback function that receives selection dataReturns:void
Example:
editor.setOnReplaceSelectedText((data) => {
const replacement = data.text.toUpperCase();
editor.replaceSelectedText(replacement);
});
Replaces selected text with new text. Requires selection data from the text:selected event.
replaceSelectedText(newText, selectionData)
Parameters:
newText
(string) - Text to replace selection withselectionData
(object) - Selection data from text:selected event or lastSelectionDataReturns:void
Example:
// Listen for text selection and wrap in bold
editor.events.on('text:selected', (data) => {
editor.replaceSelectedText(`**${data.text}**`, data);
});
// Or use lastSelectionData if availableif (editor.lastSelectionData) {
const text = editor.lastSelectionData.text;
editor.replaceSelectedText(`**${text}**`, editor.lastSelectionData);
}
Registers a custom content renderer.
registerRenderer(type, config)
Parameters:
type
(string) - Content type identifierconfig
(Object) - Renderer configuration with properties:
name
(string) - Display namerender
(function) - Render functionoutputToSource
(function) - Optional. Convert output to sourcebuttons
(Array) - Optional. Custom toolbar buttonsoperations
(Object) - Optional. Button operationsReturns:void
Example:
editor.registerRenderer('json', {
name: 'JSON',
render: (content) => {
try {
const parsed = JSON.parse(content);
return`<pre>${JSON.stringify(parsed, null, 2)}</pre>`;
} catch (e) {
return`<pre class="error">${e.message}</pre>`;
}
}
});
Manually triggers content rendering.
render()
Returns:void
Example:
// Force re-render after external changes
editor.render();
Subscribes to an editor event.
on(event, handler)
Parameters:
event
(string) - Event namehandler
(function) - Event handler functionReturns:void
Example:
editor.on('content-change', (data) => {
console.log('Content changed:', data);
});
editor.on('view-change', (view) => {
console.log('View changed to:', view);
});
Unsubscribes from an editor event.
off(event, handler)
Parameters:
event
(string) - Event namehandler
(function) - Handler to removeReturns:void
Example:
consthandler = (data) => console.log(data);
editor.on('content-change', handler);
// Later...
editor.off('content-change', handler);
Emits a custom event.
emit(event, data)
Parameters:
event
(string) - Event namedata
(any) - Event dataReturns:void
Example:
editor.emit('custom-event', { message: 'Hello' });
Cleans up the editor instance and removes event listeners.
destroy()
Returns:void
Example:
// Clean up before removing from DOM
editor.destroy();
Returns the library version information.
getVersion()
Returns:Object
- Version info with version
and url
properties
Example:
const version = editor.getVersion();
console.log(`SquibView v${version.version}`);
Enables or disables line numbers display.
setLineNumbers(show, options)
Parameters:
show
(boolean) - Whether to show line numbersoptions
(Object) - Optional. Line number configuration:
start
(number) - Starting line numberminDigits
(number) - Minimum digit widthReturns:void
Example:
// Enable line numbers
editor.setLineNumbers(true);
// Enable with custom start
editor.setLineNumbers(true, { start: 100, minDigits: 3 });