Skip to content

Commit

Permalink
Add getStoredWidth to Sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
fatih-erikli committed Sep 13, 2023
1 parent db2d5c1 commit fda815a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
20 changes: 6 additions & 14 deletions src/fontra/views/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
readFromClipboard,
reversed,
writeToClipboard,
clamp,
} from "../core/utils.js";
import { themeController } from "/core/theme-settings.js";
import { showMenu, MenuItemDivider } from "/web-components/menu-panel.js";
Expand All @@ -55,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, MAX_SIDEBAR_WIDTH, MIN_SIDEBAR_WIDTH } 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 Down Expand Up @@ -378,19 +377,12 @@ export class EditorController {
}
}, 100);

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

const resizeObserver = new ResizeObserver(([element]) => {
const leftWidth = getStoredSidebarWidth("left");
const rightWidth = getStoredSidebarWidth("right");
if (element.contentRect.width < leftWidth + rightWidth + MIN_CANVAS_SPACE) {
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);
}
Expand Down
21 changes: 13 additions & 8 deletions src/fontra/views/editor/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ export class Sidebar {
);
}

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 Down Expand Up @@ -145,15 +156,9 @@ export 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;
}
this.applyWidth(width);
this.applyWidth(sidebarWidth);
}
}
}

0 comments on commit fda815a

Please sign in to comment.