Improved fix for open editors getting carried to different notetypes (#2422)

* Revert "Fix open editors getting carried over to different notetypes (#2393)"

This reverts commit bf5bcd3f52.

* Improved fix for open editors getting carried over to different notetypes

* Run ninja format
This commit is contained in:
Fabricio Duarte 2023-03-08 06:22:02 -03:00 committed by GitHub
parent e9c5d24cbe
commit fd688b5fc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 43 deletions

View file

@ -544,6 +544,7 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
setMathjaxEnabled({json.dumps(self.mw.col.get_config("renderMathjax", True))}); setMathjaxEnabled({json.dumps(self.mw.col.get_config("renderMathjax", True))});
setShrinkImages({json.dumps(self.mw.col.get_config("shrinkEditorImages", True))}); setShrinkImages({json.dumps(self.mw.col.get_config("shrinkEditorImages", True))});
setCloseHTMLTags({json.dumps(self.mw.col.get_config("closeHTMLTags", True))}); setCloseHTMLTags({json.dumps(self.mw.col.get_config("closeHTMLTags", True))});
triggerChanges();
""" """
if self.addMode: if self.addMode:

View file

@ -16,19 +16,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
function dynamicDuration(height: number): number { function dynamicDuration(height: number): number {
return 100 + Math.pow(height, 1 / 4) * 25; return 100 + Math.pow(height, 1 / 4) * 25;
} }
// If we don't transition, the editor actually takes considerably longer to create all $: duration = dynamicDuration(contentHeight);
// the fields. Because of that, we're using an imperceptible duration for the animation
// when reduced motion is enabled.
$: duration = animated ? dynamicDuration(contentHeight) : 0.01;
const size = tweened<number>(0); const size = tweened<number>(0);
async function transition(collapse: boolean): Promise<void> { async function transition(collapse: boolean): Promise<void> {
// We need to ensure collapsibleElement still exists
if (!collapsibleElement) {
return;
}
if (collapse) { if (collapse) {
contentHeight = collapsibleElement.clientHeight; contentHeight = collapsibleElement.clientHeight;
size.set(0, { size.set(0, {
@ -49,7 +41,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
$: if (collapsibleElement) { $: if (collapsibleElement) {
window.requestAnimationFrame(() => transition(collapse)); if (animated) {
transition(collapse);
} else {
collapsed = collapse;
}
} }
let collapsibleElement: HTMLElement; let collapsibleElement: HTMLElement;
@ -71,6 +67,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<div <div
bind:this={collapsibleElement} bind:this={collapsibleElement}
class="collapsible" class="collapsible"
class:animated
class:expanded class:expanded
class:full-hide={toggleDisplay} class:full-hide={toggleDisplay}
class:measuring class:measuring
@ -81,18 +78,20 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<slot {collapsed} /> <slot {collapsed} />
</div> </div>
{#if measuring} {#if animated && measuring}
<!-- Maintain document flow while collapsible height is measured --> <!-- Maintain document flow while collapsible height is measured -->
<div class="collapsible-placeholder" /> <div class="collapsible-placeholder" />
{/if} {/if}
<style lang="scss"> <style lang="scss">
.collapsible { .collapsible {
&.animated {
&.measuring { &.measuring {
display: initial; display: initial;
position: absolute; position: absolute;
opacity: 0; opacity: 0;
} }
&.transitioning { &.transitioning {
overflow: hidden; overflow: hidden;
height: var(--height); height: var(--height);
@ -103,6 +102,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
display: initial; display: initial;
} }
} }
}
&.full-hide { &.full-hide {
&.hidden { &.hidden {
display: none; display: none;

View file

@ -130,10 +130,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
let fieldsCollapsed: boolean[] = []; let fieldsCollapsed: boolean[] = [];
export function setCollapsed(defaultCollapsed: boolean[]): void { export function setCollapsed(defaultCollapsed: boolean[]): void {
tick().then(() => {
fieldsCollapsed = fieldsCollapsed =
sessionOptions[notetypeId!]?.fieldsCollapsed ?? defaultCollapsed; sessionOptions[notetypeId!]?.fieldsCollapsed ?? defaultCollapsed;
});
} }
let richTextsHidden: boolean[] = []; let richTextsHidden: boolean[] = [];
@ -141,7 +139,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
let plainTextDefaults: boolean[] = []; let plainTextDefaults: boolean[] = [];
export function setPlainTexts(defaultPlainTexts: boolean[]): void { export function setPlainTexts(defaultPlainTexts: boolean[]): void {
tick().then(() => {
const states = sessionOptions[notetypeId!]?.fieldStates; const states = sessionOptions[notetypeId!]?.fieldStates;
if (states) { if (states) {
richTextsHidden = states.richTextsHidden; richTextsHidden = states.richTextsHidden;
@ -152,6 +149,18 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
richTextsHidden = [...defaultPlainTexts]; richTextsHidden = [...defaultPlainTexts];
plainTextsHidden = Array.from(defaultPlainTexts, (v) => !v); plainTextsHidden = Array.from(defaultPlainTexts, (v) => !v);
} }
}
export function triggerChanges(): void {
// I know this looks quite weird and doesn't seem to do anything
// but if we don't call this after setPlainTexts() and setCollapsed()
// when switching notetypes, existing collapsibles won't react
// automatically to the updated props
tick().then(() => {
fieldsCollapsed = fieldsCollapsed;
plainTextDefaults = plainTextDefaults;
richTextsHidden = richTextsHidden;
plainTextsHidden = plainTextsHidden;
}); });
} }
@ -175,7 +184,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
export function focusField(index: number | null): void { export function focusField(index: number | null): void {
setTimeout(() => { tick().then(() => {
if (typeof index === "number") { if (typeof index === "number") {
if (!(index in fields)) { if (!(index in fields)) {
return; return;
@ -353,6 +362,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
setInsertSymbolsEnabled, setInsertSymbolsEnabled,
setShrinkImages, setShrinkImages,
setCloseHTMLTags, setCloseHTMLTags,
triggerChanges,
...oldEditorAdapter, ...oldEditorAdapter,
}); });

View file

@ -16,11 +16,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const stylesDidLoad: Promise<unknown> = Promise.all([userBaseStyle, userBaseRule]); const stylesDidLoad: Promise<unknown> = Promise.all([userBaseStyle, userBaseRule]);
userBaseStyle.then((baseStyle: StyleObject) => { userBaseStyle.then((baseStyle: StyleObject) => {
const sheet = baseStyle.element.sheet; const sheet = baseStyle.element.sheet as CSSStyleSheet;
if (sheet) {
const baseIndex = sheet.insertRule("anki-editable {}"); const baseIndex = sheet.insertRule("anki-editable {}");
userBaseRuleResolve(sheet.cssRules[baseIndex] as CSSStyleRule); userBaseRuleResolve(sheet.cssRules[baseIndex] as CSSStyleRule);
}
}); });
export let color: string; export let color: string;