anki/ts/lib/styling.ts
Henrik Giesel de2cc20c59
Change how resizable images work (#1859)
* Add ResizableImage.svelte in ts/editable

* Set image constrained via attributes instead of managed style sheet

* Implement new constrained size method

* Turn WithImageConstrained.svelte into normal ts file

* Introduce removeStyleProperties

Removes "style" attribute if all style properties were cleared

* Avoid --editor-width and use one variable set on container

* Disable shrinking if already smaller than shrunken size

* Add button to restore image to original size

* Don't allow restoring original size if no custom width set

* Bottom-center HandleLabel

* Satisfy svelte-check
2022-05-13 12:57:07 +10:00

32 lines
872 B
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
/**
* @returns True, if element has no style attribute (anymore).
*/
function removeEmptyStyle(element: HTMLElement | SVGElement): boolean {
if (element.style.cssText.length === 0) {
element.removeAttribute("style");
// Calling `.hasAttribute` right after `.removeAttribute` might return true.
return true;
}
return false;
}
/**
* Will remove the style attribute, if all properties were removed.
*
* @returns True, if element has no style attributes anymore
*/
export function removeStyleProperties(
element: HTMLElement | SVGElement,
...props: string[]
): boolean {
for (const prop of props) {
element.style.removeProperty(prop);
}
return removeEmptyStyle(element);
}