Skip to content

Commit

Permalink
lists fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaRickli committed Aug 18, 2023
1 parent b37646b commit 0678bbe
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ export class InlineEditor {
target,
markdown,
parse,
clickOutside = document,
}: {
target: HTMLElement | Element | Document | ShadowRoot;
markdown: string;
parse: (md: string) => string;
/** @default document */
clickOutside?: HTMLElement | Element | Document | ShadowRoot;
}) {
this.parse = parse;
this.originalMarkdown = markdown;
Expand All @@ -44,7 +47,8 @@ export class InlineEditor {

this.target = normalizeElement(target);
this.target.innerHTML = this.parse(markdown);
this.target.addEventListener("click", () => {

clickOutside.addEventListener("click", () => {
if (typeof this.activeItem === "number") this.saveChanges();
});

Expand Down Expand Up @@ -116,17 +120,18 @@ export class InlineEditor {

private enableEditor(el: HTMLElement, index = getIndex(el)) {
el.removeEventListener("click", (ev: MouseEvent) => this.handleClick(ev));
el.innerText = this.lines[index]?.replace(/\\n/gm, "<br />") || "";
el.innerText = this.lines[index] || "";
el.setAttribute("role", "textbox");
el.contentEditable = "true";
el.style.whiteSpace = "pre-wrap";
el.focus();
}

private saveChanges(index: number = this.activeItem) {
if (typeof index !== "number") return;

const el = this.target.children.item(index) as HTMLElement;
const rawContent = el.innerText.replace(/<br ?\/?>/gm, "\n").trim();
const rawContent = el.innerText;
const content = this.parse(rawContent);

if (whitelineRegex.test(content)) {
Expand Down

0 comments on commit 0678bbe

Please sign in to comment.