mirror of
https://github.com/muerwre/markdown-home-tab.git
synced 2025-04-25 00:46:41 +07:00
initial commit
This commit is contained in:
commit
9a4eb6ef58
23 changed files with 2490 additions and 0 deletions
|
@ -0,0 +1,55 @@
|
|||
import { DockviewApi, DockviewReadyEvent } from "dockview";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { createDefaultLayout } from "../utils/createDefaultLayout";
|
||||
|
||||
export const useGridLayoutPersistance = () => {
|
||||
const api = useRef<DockviewApi>();
|
||||
|
||||
const onReady = (event: DockviewReadyEvent) => {
|
||||
api.current = event.api;
|
||||
|
||||
const layoutString = localStorage.getItem("dockview_persistance_layout");
|
||||
|
||||
if (!layoutString) {
|
||||
createDefaultLayout(event.api);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const layout = JSON.parse(layoutString);
|
||||
event.api.fromJSON(layout);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
createDefaultLayout(event.api);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!api.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
const disposable = api.current.onDidLayoutChange(() => {
|
||||
if (!api.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!api.current.groups.length) {
|
||||
createDefaultLayout(api.current);
|
||||
}
|
||||
|
||||
const layout = api.current.toJSON();
|
||||
|
||||
localStorage.setItem(
|
||||
"dockview_persistance_layout",
|
||||
JSON.stringify(layout)
|
||||
);
|
||||
});
|
||||
|
||||
return () => {
|
||||
disposable.dispose();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { api, onReady };
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue