Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work in progress: Implement MIN_CANVAS_SPACE #799

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/fontra/views/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
import { staticGlyphToGLIF } from "../core/glyph-glif.js";
import { pathToSVG } from "../core/glyph-svg.js";
import { parseClipboard } from "../core/server-utils.js";
import Sidebar from "./sidebar.js";
import { Sidebar, MIN_SIDEBAR_WIDTH } from "./sidebar.js";

import TextEntryPanel from "./panel-text-entry.js";
import GlyphSearchPanel from "./panel-glyph-search.js";
Expand All @@ -63,6 +63,8 @@ import UserSettingsPanel from "./panel-user-settings.js";
import ReferenceFontPanel from "./panel-reference-font.js";
import SelectionInfoPanel from "./panel-selection-info.js";

const MIN_CANVAS_SPACE = 200;

export class EditorController {
static async fromWebSocket() {
const pathItems = window.location.pathname.split("/").slice(3);
Expand Down Expand Up @@ -374,6 +376,19 @@ export class EditorController {
sidebarContainer.classList.add("animating");
}
}, 100);

const resizeObserver = new ResizeObserver(([element]) => {
const totalWidth = this.sidebars.reduce(
(total, sidebar) => total + sidebar.getStoredWidth(),
0
);
if (element.contentRect.width < totalWidth + MIN_CANVAS_SPACE) {
for (const sidebar of this.sidebars) {
sidebar.applyWidth(MIN_SIDEBAR_WIDTH, true);
}
}
});
resizeObserver.observe(document.documentElement);
}

addSidebar(sidebar) {
Expand Down
42 changes: 27 additions & 15 deletions src/fontra/views/editor/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as html from "/core/unlit.js";
import { clamp } from "../../core/utils.js";

const MIN_SIDEBAR_WIDTH = 200;
const MAX_SIDEBAR_WIDTH = 500;
export const MIN_SIDEBAR_WIDTH = 200;
export const MAX_SIDEBAR_WIDTH = 500;

export default class Sidebar {
export class Sidebar {
constructor(identifier) {
this.identifier = identifier;
this.container = null;
Expand Down Expand Up @@ -92,6 +92,27 @@ export default class Sidebar {
this.initResizeGutter();
}

applyWidth(width, saveLocalStorage = false) {
if (saveLocalStorage) {
localStorage.setItem(`fontra-sidebar-width-${this.identifier}`, width);
}
document.documentElement.style.setProperty(
`--sidebar-content-width-${this.identifier}`,
`${width}px`
);
}

getStoredWidth() {
const sidebarWidth = localStorage.getItem(
`fontra-sidebar-width-${this.identifier}`
);
let width = clamp(parseInt(sidebarWidth), MIN_SIDEBAR_WIDTH, MAX_SIDEBAR_WIDTH);
if (isNaN(width)) {
width = MIN_SIDEBAR_WIDTH;
}
return width;
}

initResizeGutter() {
let initialWidth;
let initialPointerCoordinateX;
Expand All @@ -113,7 +134,7 @@ export default class Sidebar {
}
};
const onPointerUp = () => {
localStorage.setItem(`fontra-sidebar-width-${this.identifier}`, width);
this.applyWidth(width, true);
sidebarResizing.classList.add("animating");
sidebarResizing = undefined;
initialWidth = undefined;
Expand All @@ -135,18 +156,9 @@ export default class Sidebar {
document.addEventListener("pointermove", onPointerMove);
document.addEventListener("pointerup", onPointerUp, { once: true });
});
const sidebarWidth = localStorage.getItem(
`fontra-sidebar-width-${this.identifier}`
);
const sidebarWidth = this.getStoredWidth();
if (sidebarWidth) {
let width = clamp(parseInt(sidebarWidth), MIN_SIDEBAR_WIDTH, MAX_SIDEBAR_WIDTH);
if (isNaN(width)) {
width = MIN_SIDEBAR_WIDTH;
}
document.documentElement.style.setProperty(
`--sidebar-content-width-${this.identifier}`,
`${width}px`
);
this.applyWidth(sidebarWidth);
}
}
}