Skip to content
Open
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
49 changes: 45 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class Tooltip {
this.button = null;
this._state = false;
this.spanTooltip = null;
this.eventListenerAdded = false;

const { location = 'bottom' } = config;
this.tooltipLocation = location;
Expand Down Expand Up @@ -289,28 +290,68 @@ export default class Tooltip {
*/
showActions() {
this.tooltipInput.hidden = false;
this.api.listeners.on(this.tooltipInput, 'keydown', (e) => {

// Remove any existing event listener to prevent duplicates
if (this.eventListenerAdded && this.tooltipInputHandler) {
this.api.listeners.off(this.tooltipInput, 'keydown', this.tooltipInputHandler);
}

// Create a bound handler function
this.tooltipInputHandler = (e) => {
if (e.key === 'Enter') {
e.preventDefault();
e.stopPropagation();
const tooltipValue = this.tooltipInput.value;
this.createTooltip(tooltipValue);
this.closeToolbar();
}
}, false);
};

// Add the event listener
this.api.listeners.on(this.tooltipInput, 'keydown', this.tooltipInputHandler, false);
this.eventListenerAdded = true;

// Focus the input with a small delay to ensure it's visible
setTimeout(() => {
if (this.tooltipInput) {
this.tooltipInput.focus();
// Move cursor to end of input
this.tooltipInput.setSelectionRange(this.tooltipInput.value.length, this.tooltipInput.value.length);
}
}, 10);
}

/**
* Hide the input if the user do not have tooltip in the selected text
*/
hideActions() {
this.tooltipInput.hidden = true;

// Clean up event listener when hiding
if (this.eventListenerAdded && this.tooltipInputHandler) {
this.api.listeners.off(this.tooltipInput, 'keydown', this.tooltipInputHandler);
this.eventListenerAdded = false;
}
}

/**
* close the toolbar when the user presses Enter
*/
closeToolbar() {
const toolbar = document.querySelector('.ce-inline-toolbar--showed');
toolbar.classList.remove('ce-inline-toolbar--showed');
if (toolbar) {
toolbar.classList.remove('ce-inline-toolbar--showed');
}
}

/**
* Clear method to clean up event listeners when tool is destroyed
*/
clear() {
if (this.eventListenerAdded && this.tooltipInputHandler && this.tooltipInput) {
this.api.listeners.off(this.tooltipInput, 'keydown', this.tooltipInputHandler);
this.eventListenerAdded = false;
}
}

/**
Expand All @@ -324,4 +365,4 @@ export default class Tooltip {
},
};
}
}
}