Skip to content

Commit

Permalink
fix: Fixes issue with Open/Collapse Admonition commands
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Jan 30, 2024
1 parent 8952e8e commit 6d61851
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
49 changes: 28 additions & 21 deletions src/callout/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,30 +157,37 @@ export default class CalloutManager extends Component {
}
}
titleEl.onclick = (event: MouseEvent) => {
event.preventDefault();
this.collapse(callout, event);
};
}

function transitionEnd(evt: TransitionEvent) {
content.removeEventListener("transitionend", transitionEnd);
content.style.removeProperty("transition");
}
content.addEventListener("transitionend", transitionEnd);
content.style.setProperty(
"transition",
"all 100ms cubic-bezier(.02, .01, .47, 1)"
);
collapsed = callout.hasClass("is-collapsed");
if (event.button == 0) {
for (const prop of this.heights) {
const heights = this.getComputedHeights(content);
content.style.setProperty(
prop,
collapsed ? heights[prop] : "0px"
);
}
collapse(callout: HTMLElement, event?: MouseEvent) {
event?.preventDefault();
const content =
callout.querySelector<HTMLDivElement>(".callout-content");

function transitionEnd(evt: TransitionEvent) {
content.removeEventListener("transitionend", transitionEnd);
content.style.removeProperty("transition");
}
content.addEventListener("transitionend", transitionEnd);
content.style.setProperty(
"transition",
"all 100ms cubic-bezier(.02, .01, .47, 1)"
);
let collapsed = callout.hasClass("is-collapsed");

callout.toggleClass("is-collapsed", !collapsed);
if (!event || event.button == 0) {
const heights = this.getComputedHeights(content);
for (const prop of this.heights) {
content.style.setProperty(
prop,
collapsed ? heights[prop] : "0px"
);
}
};

callout.toggleClass("is-collapsed", !collapsed);
}
}

getComputedHeights(el: HTMLDivElement): Heights {
Expand Down
19 changes: 11 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,13 @@ export default class ObsidianAdmonition extends Plugin {
this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view || !(view instanceof MarkdownView)) return;

let admonitions = view.contentEl.querySelectorAll(
"details[open].admonition-plugin"
);
let admonitions =
view.contentEl.querySelectorAll<HTMLElement>(
".callout.is-collapsible:not(.is-collapsed)"
);
for (let i = 0; i < admonitions.length; i++) {
let admonition = admonitions[i];
admonition.removeAttribute("open");
this.calloutManager.collapse(admonition);
}
}
});
Expand All @@ -201,12 +202,14 @@ export default class ObsidianAdmonition extends Plugin {
this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view || !(view instanceof MarkdownView)) return;

let admonitions = view.contentEl.querySelectorAll(
"details:not([open]).admonition-plugin"
);
let admonitions =
view.contentEl.querySelectorAll<HTMLElement>(
".callout.is-collapsible.is-collapsed"
);
for (let i = 0; i < admonitions.length; i++) {
let admonition = admonitions[i];
admonition.setAttribute("open", "open");

this.calloutManager.collapse(admonition);
}
}
});
Expand Down

0 comments on commit 6d61851

Please sign in to comment.