Skip to content

Commit 6d61851

Browse files
committed
fix: Fixes issue with Open/Collapse Admonition commands
1 parent 8952e8e commit 6d61851

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

src/callout/manager.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -157,30 +157,37 @@ export default class CalloutManager extends Component {
157157
}
158158
}
159159
titleEl.onclick = (event: MouseEvent) => {
160-
event.preventDefault();
160+
this.collapse(callout, event);
161+
};
162+
}
161163

162-
function transitionEnd(evt: TransitionEvent) {
163-
content.removeEventListener("transitionend", transitionEnd);
164-
content.style.removeProperty("transition");
165-
}
166-
content.addEventListener("transitionend", transitionEnd);
167-
content.style.setProperty(
168-
"transition",
169-
"all 100ms cubic-bezier(.02, .01, .47, 1)"
170-
);
171-
collapsed = callout.hasClass("is-collapsed");
172-
if (event.button == 0) {
173-
for (const prop of this.heights) {
174-
const heights = this.getComputedHeights(content);
175-
content.style.setProperty(
176-
prop,
177-
collapsed ? heights[prop] : "0px"
178-
);
179-
}
164+
collapse(callout: HTMLElement, event?: MouseEvent) {
165+
event?.preventDefault();
166+
const content =
167+
callout.querySelector<HTMLDivElement>(".callout-content");
168+
169+
function transitionEnd(evt: TransitionEvent) {
170+
content.removeEventListener("transitionend", transitionEnd);
171+
content.style.removeProperty("transition");
172+
}
173+
content.addEventListener("transitionend", transitionEnd);
174+
content.style.setProperty(
175+
"transition",
176+
"all 100ms cubic-bezier(.02, .01, .47, 1)"
177+
);
178+
let collapsed = callout.hasClass("is-collapsed");
180179

181-
callout.toggleClass("is-collapsed", !collapsed);
180+
if (!event || event.button == 0) {
181+
const heights = this.getComputedHeights(content);
182+
for (const prop of this.heights) {
183+
content.style.setProperty(
184+
prop,
185+
collapsed ? heights[prop] : "0px"
186+
);
182187
}
183-
};
188+
189+
callout.toggleClass("is-collapsed", !collapsed);
190+
}
184191
}
185192

186193
getComputedHeights(el: HTMLDivElement): Heights {

src/main.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,13 @@ export default class ObsidianAdmonition extends Plugin {
177177
this.app.workspace.getActiveViewOfType(MarkdownView);
178178
if (!view || !(view instanceof MarkdownView)) return;
179179

180-
let admonitions = view.contentEl.querySelectorAll(
181-
"details[open].admonition-plugin"
182-
);
180+
let admonitions =
181+
view.contentEl.querySelectorAll<HTMLElement>(
182+
".callout.is-collapsible:not(.is-collapsed)"
183+
);
183184
for (let i = 0; i < admonitions.length; i++) {
184185
let admonition = admonitions[i];
185-
admonition.removeAttribute("open");
186+
this.calloutManager.collapse(admonition);
186187
}
187188
}
188189
});
@@ -201,12 +202,14 @@ export default class ObsidianAdmonition extends Plugin {
201202
this.app.workspace.getActiveViewOfType(MarkdownView);
202203
if (!view || !(view instanceof MarkdownView)) return;
203204

204-
let admonitions = view.contentEl.querySelectorAll(
205-
"details:not([open]).admonition-plugin"
206-
);
205+
let admonitions =
206+
view.contentEl.querySelectorAll<HTMLElement>(
207+
".callout.is-collapsible.is-collapsed"
208+
);
207209
for (let i = 0; i < admonitions.length; i++) {
208210
let admonition = admonitions[i];
209-
admonition.setAttribute("open", "open");
211+
212+
this.calloutManager.collapse(admonition);
210213
}
211214
}
212215
});

0 commit comments

Comments
 (0)