/
';
figure.parentNode?.insertBefore(p, figure.nextSibling);
after = p;
}
const range = document.createRange();
range.setStart(after, 0);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
private applyImage() {
const raw = this.srcValue.trim();
if (!raw) {
if (this.uploadFromComputer) {
this.errorMessage = 'Enter an image link or upload a file';
}
return;
}
const src = this.normalizeSrc(raw);
if (!src) {
this.errorMessage = 'Enter a valid image link';
return;
}
if (this.activeFigure) {
const img = this.activeFigure.querySelector('img');
if (img) {
img.src = src;
if (this.altValue.trim()) {
img.alt = this.altValue.trim();
} else {
img.removeAttribute('alt');
}
}
let figcaption = this.activeFigure.querySelector('figcaption');
if (this.captionValue.trim()) {
if (!figcaption) {
figcaption = document.createElement('figcaption');
this.activeFigure.appendChild(figcaption);
}
figcaption.textContent = this.captionValue.trim();
} else {
figcaption?.remove();
}
this.emit(src);
this.closePopover();
requestAnimationFrame(() => this.updateOverlayPosition());
return;
}
if (!(this.selectionRange instanceof Range)) return;
this.editorEl.focus();
const sel = document.getSelection();
if (!sel) return;
sel.removeAllRanges();
sel.addRange(this.selectionRange);
const figure = this.buildFigure(src, this.altValue, this.captionValue);
this.insertFigureAt(this.selectionRange, figure);
this.emit(src);
this.closePopover();
}
private removeImage() {
if (!this.activeFigure) return;
this.deselectImage();
const parent = this.activeFigure.parentNode;
this.activeFigure.remove();
if (parent instanceof HTMLElement && !parent.hasChildNodes()) {
parent.innerHTML = '
';
}
this.emit('');
this.closePopover();
}
private emit(src: string) {
this.dispatchEvent(
new CustomEvent('nile-image-changed', {
detail: {
src,
alt: this.altValue.trim(),
caption: this.captionValue.trim(),
},
bubbles: true,
composed: true,
})
);
}
render() {
const iconColor = this.disabled
? 'var(--nile-colors-neutral-500, var(--ng-colors-fg-disabled-subtle))'
: 'var(--nile-colors-dark-900, var(--ng-colors-text-primary-900))';
return html`