Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function insertNewLineAfterBlotAndTrim(quill, range: RangeStatic, deleteAmount = 1) {
const [line, offset] = quill.getLine(range.index);
const newBlot = new BlockBlot(BlockBlot.create(""));
const thisBlot = line;
const nextBlot = thisBlot.next;
newBlot.insertInto(quill.scroll, nextBlot);
// Now we need to clean up that extra newline.
const positionUpToPreviousNewline = range.index + line.length() - offset;
const deleteDelta = new Delta().retain(positionUpToPreviousNewline - deleteAmount).delete(deleteAmount);
quill.updateContents(deleteDelta, Emitter.sources.USER);
quill.setSelection(positionUpToPreviousNewline - deleteAmount, Emitter.sources.USER);
}
onImageUploadFailure = (file, error) => {
logError(error.message);
if (file == null) {
this.quill.insertEmbed(this.lastSelection.index, "embed-error", { errors: [error] }, Emitter.sources.USER);
return;
}
const errorBlot = Parchment.create("embed-error", { errors: [error] }, Emitter.sources.USER);
const loadingBlot = this.currentUploads.get(file);
// The loading blot may have been undone/deleted since we created it.
if (loadingBlot) {
loadingBlot.replaceWith(errorBlot);
}
loadingBlot.replaceWith(errorBlot);
this.currentUploads.delete(file);
};
this.quill.on(Emitter.events.EDITOR_CHANGE, (type, range, oldRange, source) => {
if (type !== Emitter.events.SELECTION_CHANGE) return;
if (range != null && range.length > 0 && source === Emitter.sources.USER) {
this.show();
// Lock our width so we will expand beyond our offsetParent boundaries
this.root.style.left = '0px';
this.root.style.width = '';
this.root.style.width = this.root.offsetWidth + 'px';
let lines = this.quill.getLines(range.index, range.length);
if (lines.length === 1) {
this.position(this.quill.getBounds(range));
} else {
let lastLine = lines[lines.length - 1];
let index = this.quill.getIndex(lastLine);
let length = Math.min(lastLine.length() - 1, range.index + range.length - index);
let bounds = this.quill.getBounds(new Range(index, length));
this.position(bounds);
}
} else if (document.activeElement !== this.textbox && this.quill.hasFocus()) {
export function stripFormattingFromFirstBlot(quill: Quill) {
const [firstBlot] = quill.getLine(0);
const blotName = (firstBlot.constructor as any).blotName;
const delta = new Delta().retain(firstBlot.length(), { [blotName]: false });
quill.updateContents(delta, Emitter.sources.USER);
}
this.root.querySelector('a.ql-remove').addEventListener('click', (event) => {
if (this.linkRange != null) {
let range = this.linkRange;
this.restoreFocus();
this.quill.formatText(range, 'link', false, Emitter.sources.USER);
delete this.linkRange;
}
event.preventDefault();
this.hide();
});
this.quill.on(Emitter.events.SELECTION_CHANGE, (range, oldRange, source) => {
private reset = () => {
this.props.lastGoodSelection && this.quill.setSelection(this.props.lastGoodSelection, Emitter.sources.USER);
this.setState({
inputValue: "",
});
};
const isFirstLineSelected = selection.index === 0;
const selectionIsEntireScroll = isFirstLineSelected;
const blotMatches =
line instanceof LineBlot || line instanceof CodeBlockBlot || line instanceof FocusableEmbedBlot;
if ((rangeStartsBeforeSelection || rangeEndsAfterSelection || selectionIsEntireScroll) && blotMatches) {
let delta = new Delta();
const newSelection = range;
if (isFirstLineSelected) {
delta = delta.insert("\n");
newSelection.length += 1;
}
this.quill.updateContents(delta, Emitter.sources.USER);
this.quill.setSelection(newSelection);
stripFormattingFromFirstBlot(this.quill);
this.quill.setSelection(newSelection);
}
return true;
};
reader.onload = (e) => {
let range = this.quill.getSelection(true);
this.quill.updateContents(new Delta()
.retain(range.index)
.delete(range.length)
.insert({ image: e.target.result })
, Emitter.sources.USER);
this.quill.setSelection(range.index + 1, Emitter.sources.SILENT);
fileInput.value = "";
}
reader.readAsDataURL(fileInput.files[0]);
reader.onload = (e) => {
let range = this.quill.getSelection(true);
this.quill.updateContents(new Delta()
.retain(range.index)
.delete(range.length)
.insert({ image: e.target.result })
, Emitter.sources.USER);
this.quill.setSelection(range.index + 1, Emitter.sources.SILENT);
fileInput.value = "";
}
reader.readAsDataURL(fileInput.files[0]);
onImageUploadStart = (file) => {
this.quill.insertEmbed(this.lastSelection.index, "embed-loading", {}, Emitter.sources.USER);
const [blot] = this.quill.getLine(this.lastSelection.index);
this.insertTrailingNewline();
blot.registerDeleteCallback(() => {
if (this.currentUploads.has(file)) {
this.currentUploads.delete(file);
}
});
this.currentUploads.set(file, blot);
};