Fix and refactor image size constraints (#2318)

* Remove 'ResizableImage.svelte'

Rather than having a svelte file that just sets global styles, it's
better to set those in a (s)css file.

* Fix and refactor image size constraints

- Fixes https://forums.ankiweb.net/t/2-1-56-image-size-problem-in-editor/26207
- Use :host-context() to switch image styles instead of toggling CSS
variables.

* Fix toolbar buttons for image overlay sometimes being cut off
This commit is contained in:
Hikaru Y 2023-01-13 22:18:25 +09:00 committed by GitHub
parent 8131ea1908
commit 604e0f46e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 50 deletions

View file

@ -1,19 +0,0 @@
<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<style lang="scss">
:global(img:not(.mathjax)) {
max-width: var(--editor-default-max-width, inherit);
max-height: var(--editor-default-max-height, inherit);
&:is([data-editor-shrink="true"]) {
max-width: var(--editor-shrink-max-width);
max-height: var(--editor-shrink-max-height);
}
&:is([data-editor-shrink="false"]) {
max-width: initial;
max-height: initial;
}
}
</style>

View file

@ -26,3 +26,18 @@ p {
pre {
white-space: pre-wrap;
}
// image size constraints
img:not(.mathjax) {
&:not([data-editor-shrink="false"]) {
:host-context(.shrink-image) & {
max-width: var(--editor-default-max-width);
max-height: var(--editor-default-max-height);
}
}
&[data-editor-shrink="true"] {
max-width: var(--editor-shrink-max-width);
max-height: var(--editor-shrink-max-height);
}
}

View file

@ -5,4 +5,3 @@ import "./editable-base.scss";
/* only imported for the CSS */
import "./ContentEditable.svelte";
import "./Mathjax.svelte";
import "./ResizableImage.svelte";

View file

@ -24,25 +24,26 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import HandleControl from "../HandleControl.svelte";
import HandleLabel from "../HandleLabel.svelte";
import { context } from "../NoteEditor.svelte";
import type { RichTextInputAPI } from "../rich-text-input";
import {
editingInputIsRichText,
lifecycle as richTextLifecycle,
} from "../rich-text-input";
import { editingInputIsRichText } from "../rich-text-input";
import FloatButtons from "./FloatButtons.svelte";
import SizeSelect from "./SizeSelect.svelte";
export let maxWidth: number;
export let maxHeight: number;
richTextLifecycle.onMount(({ element }: RichTextInputAPI): void => {
(async () => {
const container = await element;
(<[string, number][]>[
["--editor-shrink-max-width", maxWidth],
["--editor-shrink-max-height", maxHeight],
["--editor-default-max-width", maxWidth],
["--editor-default-max-height", maxHeight],
]).forEach(([prop, value]) =>
document.documentElement.style.setProperty(prop, `${value}px`),
);
container.style.setProperty("--editor-shrink-max-width", `${maxWidth}px`);
container.style.setProperty("--editor-shrink-max-height", `${maxHeight}px`);
})();
});
$: document.documentElement.classList.toggle(
"shrink-image",
$shrinkImagesByDefault,
);
const { focusedInput } = context.get();
@ -83,24 +84,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
}
}
$: {
if ($shrinkImagesByDefault) {
document.documentElement.style.setProperty(
"--editor-default-max-width",
`${maxWidth}px`,
);
document.documentElement.style.setProperty(
"--editor-default-max-height",
`${maxHeight}px`,
);
} else {
document.documentElement.style.removeProperty("--editor-default-max-width");
document.documentElement.style.removeProperty(
"--editor-default-max-height",
);
}
}
async function resetHandle(): Promise<void> {
activeImage = null;
await tick();
@ -265,6 +248,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
offset={20}
inline
hideIfReferenceHidden
portalTarget={document.body}
let:position={positionFloating}
on:close={async ({ detail }) => {
const { reason, originalEvent } = detail;