Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/overtype-webcomponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,18 @@ class OverTypeEditor extends HTMLElement {
getEditor() {
return this._editor;
}

showToolbar() {
if (this._editor) {
this._editor.showToolbar();
}
}

hideToolbar() {
if (this._editor) {
this._editor.hideToolbar();
}
}
}

// Register the custom element
Expand Down
24 changes: 24 additions & 0 deletions src/overtype.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,30 @@ class OverType {
this.updatePreview();
}


/**
* reveal the toolbar if it is hidden
* create it if it does not exist
* @returns {void}
*/
showToolbar(){
if (this.toolbar) {
this.toolbar.show();
} else {
this._createToolbar();
}
}

/**
* hide the toolbar if it exists
* @returns {void}
*/
hideToolbar(){
if (this.toolbar) {
this.toolbar.hide();
}
}

/**
* Set theme for this instance
* @param {string|Object} theme - Theme name or custom theme object
Expand Down
21 changes: 21 additions & 0 deletions src/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,27 @@ export class Toolbar {
}
}

/**
* Show the toolbar
*/
show() {
if (this.container) {
this.container.classList.add('overtype-toolbar');
this.container.style.display = '';
}
}

/**
* Hide the toolbar, but do not destroy it
*/
hide() {
if (this.container) {
this.container.style.display = 'none';
// .overtype-toolbar will override display:none, so we remove it
this.container.classList.remove('overtype-toolbar');
}
}

/**
* Destroy toolbar and cleanup
*/
Expand Down
29 changes: 29 additions & 0 deletions test/webcomponent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,5 +467,34 @@ setTimeout(() => {
}
}, 100);
});


runTest('Show / hide toolbar programmatically via API', () => {
const container = dom.window.document.getElementById('test-container');
const editor = dom.window.document.createElement('overtype-editor');
container.appendChild(editor);

setTimeout(() => {

// Initially no toolbar
let instance = editor.getEditor();
if (instance.toolbar) throw new Error('Toolbar should not be present initially');

// Show toolbar
editor.showToolbar();
instance = editor.getEditor();
if (!instance.toolbar || !instance.toolbar.container.classList.contains('overtype-toolbar')) throw new Error('Toolbar should be present and visible after showToolbar()');

// Hide toolbar
editor.hideToolbar();
instance = editor.getEditor();
if (instance.toolbar.container.classList.contains('overtype-toolbar')) throw new Error('Toolbar should not be visible after hideToolbar()');

container.removeChild(editor);
console.log(' ✅ Toolbar show/hide API works correctly');

}, 100); // Allow initialization
});


}, 200);