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

* Fix open editors getting carried over to different notetypes

* Fix first field not getting automatically focused

* Fix collapsibles not transitioning in reduced motion mode

* Fix editor taking a longer time to start when reduced motion is enabled

If we don't transition, the editor actually takes considerably longer to create all the fields.

* Fix fields not collapsing when notetype is loaded
This commit is contained in:
Fabricio Duarte 2023-02-27 03:49:48 -03:00 committed by GitHub
parent b12476de9a
commit bf5bcd3f52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 39 deletions

View file

@ -16,11 +16,19 @@ 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;
} }
$: duration = dynamicDuration(contentHeight); // If we don't transition, the editor actually takes considerably longer to create all
// 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, {
@ -41,11 +49,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
$: if (collapsibleElement) { $: if (collapsibleElement) {
if (animated) { window.requestAnimationFrame(() => transition(collapse));
transition(collapse);
} else {
collapsed = collapse;
}
} }
let collapsibleElement: HTMLElement; let collapsibleElement: HTMLElement;
@ -67,7 +71,6 @@ 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
@ -78,20 +81,18 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<slot {collapsed} /> <slot {collapsed} />
</div> </div>
{#if animated && measuring} {#if 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);
@ -102,7 +103,6 @@ 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,8 +130,10 @@ 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[] = [];
@ -139,6 +141,7 @@ 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;
@ -149,6 +152,7 @@ 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);
} }
});
} }
function setMathjaxEnabled(enabled: boolean): void { function setMathjaxEnabled(enabled: boolean): void {
@ -171,7 +175,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 {
tick().then(() => { setTimeout(() => {
if (typeof index === "number") { if (typeof index === "number") {
if (!(index in fields)) { if (!(index in fields)) {
return; return;

View file

@ -16,9 +16,11 @@ 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 as CSSStyleSheet; const sheet = baseStyle.element.sheet;
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;