layout-ui.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. (function () {
  2. const storageKey = "oas_annotator_layout_v3";
  3. const themeKey = "oas_annotator_theme_v1";
  4. const leftWindow = document.getElementById("leftWindow");
  5. const rightWindow = document.getElementById("rightWindow");
  6. const leftContent = document.getElementById("leftContent");
  7. const rightContent = document.getElementById("rightContent");
  8. const leftStack = document.getElementById("leftStack");
  9. const rightStack = document.getElementById("rightStack");
  10. const leftSplitter = document.getElementById("leftSplitter");
  11. const rightSplitter = document.getElementById("rightSplitter");
  12. const leftPanelSplitter = document.getElementById("leftPanelSplitter");
  13. const rightPanelSplitter = document.getElementById("rightPanelSplitter");
  14. const themeSelect = document.getElementById("themeSelect");
  15. const appRoot = document.querySelector(".app");
  16. const ruleEditorPanel = document.getElementById("ruleEditorPanel");
  17. const ruleList = document.getElementById("ruleList");
  18. const ruleListSplitter = document.getElementById("ruleListSplitter");
  19. const outputDockToggle = document.getElementById("outputDockToggle");
  20. const bottomOutputDock = document.getElementById("bottomOutputDock");
  21. const bottomOutputResize = document.getElementById("bottomOutputResize");
  22. if (
  23. !leftWindow ||
  24. !rightWindow ||
  25. !leftContent ||
  26. !rightContent ||
  27. !leftStack ||
  28. !rightStack ||
  29. !leftSplitter ||
  30. !rightSplitter ||
  31. !leftPanelSplitter ||
  32. !rightPanelSplitter
  33. ) {
  34. return;
  35. }
  36. const minLeftWidth = 280;
  37. const maxLeftWidth = 760;
  38. const minRightWidth = 320;
  39. const maxRightWidth = 760;
  40. const minPanelHeight = 140;
  41. const minDockHeight = 140;
  42. const minRuleListHeight = 220;
  43. const leftButtons = Array.from(document.querySelectorAll(".rail-btn[data-window='left']"));
  44. const rightButtons = Array.from(document.querySelectorAll(".rail-btn[data-window='right']"));
  45. const leftOrder = leftButtons.map((btn) => btn.dataset.panelId).filter(Boolean);
  46. const rightOrder = rightButtons.map((btn) => btn.dataset.panelId).filter(Boolean);
  47. const defaultState = {
  48. leftWidth: 360,
  49. rightWidth: 460,
  50. leftPanels: leftOrder.slice(),
  51. rightPanels: rightOrder.slice(),
  52. leftSplitRatio: 0.5,
  53. rightSplitRatio: 0.5,
  54. outputDockOpen: false,
  55. outputDockHeight: 220,
  56. ruleListHeight: minRuleListHeight,
  57. };
  58. const state = loadState();
  59. normalizeState();
  60. applyTheme(loadTheme());
  61. applyWidths();
  62. applySide("left");
  63. applySide("right");
  64. applyBottomDock();
  65. updateRuleEditorCompactMode();
  66. applyRuleListHeight();
  67. if (ruleEditorPanel && typeof ResizeObserver !== "undefined") {
  68. const observer = new ResizeObserver(() => {
  69. updateRuleEditorCompactMode();
  70. });
  71. observer.observe(ruleEditorPanel);
  72. }
  73. leftButtons.forEach((button) => {
  74. button.addEventListener("click", () => {
  75. togglePanel("left", button.dataset.panelId || "");
  76. });
  77. });
  78. rightButtons.forEach((button) => {
  79. button.addEventListener("click", () => {
  80. togglePanel("right", button.dataset.panelId || "");
  81. });
  82. });
  83. leftSplitter.addEventListener("mousedown", (event) => {
  84. if (!state.leftPanels.length) {
  85. return;
  86. }
  87. startSideDrag("left", event);
  88. });
  89. rightSplitter.addEventListener("mousedown", (event) => {
  90. if (!state.rightPanels.length) {
  91. return;
  92. }
  93. startSideDrag("right", event);
  94. });
  95. leftPanelSplitter.addEventListener("mousedown", (event) => {
  96. if (!leftStack.classList.contains("stack-two-open")) {
  97. return;
  98. }
  99. startPanelDrag("left", event);
  100. });
  101. rightPanelSplitter.addEventListener("mousedown", (event) => {
  102. if (!rightStack.classList.contains("stack-two-open")) {
  103. return;
  104. }
  105. startPanelDrag("right", event);
  106. });
  107. if (ruleListSplitter && ruleList) {
  108. ruleListSplitter.addEventListener("mousedown", (event) => {
  109. startRuleListDrag(event);
  110. });
  111. }
  112. if (outputDockToggle && bottomOutputDock) {
  113. outputDockToggle.addEventListener("click", () => {
  114. state.outputDockOpen = !state.outputDockOpen;
  115. applyBottomDock();
  116. saveState();
  117. requestStageReflow();
  118. });
  119. }
  120. if (bottomOutputResize && bottomOutputDock) {
  121. bottomOutputResize.addEventListener("mousedown", (event) => {
  122. if (!state.outputDockOpen) {
  123. return;
  124. }
  125. startBottomDockDrag(event);
  126. });
  127. }
  128. if (themeSelect) {
  129. themeSelect.addEventListener("change", () => {
  130. const theme = normalizeTheme(themeSelect.value);
  131. applyTheme(theme);
  132. saveTheme(theme);
  133. });
  134. }
  135. window.addEventListener("resize", () => {
  136. updatePanelHeights("left", false);
  137. updatePanelHeights("right", false);
  138. updateRuleEditorCompactMode();
  139. applyRuleListHeight();
  140. if (state.outputDockOpen) {
  141. state.outputDockHeight = clamp(state.outputDockHeight, minDockHeight, getDockMaxHeight());
  142. applyBottomDock();
  143. }
  144. });
  145. function updateRuleEditorCompactMode() {
  146. if (!ruleEditorPanel) {
  147. return;
  148. }
  149. const compact = ruleEditorPanel.clientWidth > 0 && ruleEditorPanel.clientWidth < 430;
  150. ruleEditorPanel.classList.toggle("rule-editor-compact", compact);
  151. }
  152. function clamp(value, min, max) {
  153. return Math.max(min, Math.min(max, value));
  154. }
  155. function normalizeTheme(theme) {
  156. return theme === "light" ? "light" : "dark";
  157. }
  158. function loadTheme() {
  159. try {
  160. const raw = window.localStorage.getItem(themeKey);
  161. return normalizeTheme(raw || "dark");
  162. } catch (_error) {
  163. return "dark";
  164. }
  165. }
  166. function saveTheme(theme) {
  167. try {
  168. window.localStorage.setItem(themeKey, normalizeTheme(theme));
  169. } catch (_error) {
  170. // ignore
  171. }
  172. }
  173. function applyTheme(theme) {
  174. const normalized = normalizeTheme(theme);
  175. document.documentElement.setAttribute("data-theme", normalized);
  176. if (themeSelect) {
  177. themeSelect.value = normalized;
  178. }
  179. }
  180. function loadState() {
  181. try {
  182. const raw = window.localStorage.getItem(storageKey);
  183. if (!raw) {
  184. return { ...defaultState };
  185. }
  186. const parsed = JSON.parse(raw);
  187. return {
  188. leftWidth: Number(parsed.leftWidth) || defaultState.leftWidth,
  189. rightWidth: Number(parsed.rightWidth) || defaultState.rightWidth,
  190. leftPanels: Array.isArray(parsed.leftPanels) ? parsed.leftPanels.slice() : defaultState.leftPanels.slice(),
  191. rightPanels: Array.isArray(parsed.rightPanels) ? parsed.rightPanels.slice() : defaultState.rightPanels.slice(),
  192. leftSplitRatio: Number(parsed.leftSplitRatio),
  193. rightSplitRatio: Number(parsed.rightSplitRatio),
  194. outputDockOpen: Boolean(parsed.outputDockOpen),
  195. outputDockHeight: Number(parsed.outputDockHeight),
  196. ruleListHeight: Number(parsed.ruleListHeight),
  197. };
  198. } catch (_error) {
  199. return { ...defaultState };
  200. }
  201. }
  202. function saveState() {
  203. try {
  204. window.localStorage.setItem(
  205. storageKey,
  206. JSON.stringify({
  207. leftWidth: state.leftWidth,
  208. rightWidth: state.rightWidth,
  209. leftPanels: state.leftPanels,
  210. rightPanels: state.rightPanels,
  211. leftSplitRatio: state.leftSplitRatio,
  212. rightSplitRatio: state.rightSplitRatio,
  213. outputDockOpen: state.outputDockOpen,
  214. outputDockHeight: state.outputDockHeight,
  215. ruleListHeight: state.ruleListHeight,
  216. }),
  217. );
  218. } catch (_error) {
  219. // ignore
  220. }
  221. }
  222. function normalizeState() {
  223. state.leftWidth = clamp(state.leftWidth, minLeftWidth, maxLeftWidth);
  224. state.rightWidth = clamp(state.rightWidth, minRightWidth, maxRightWidth);
  225. state.leftPanels = leftOrder.filter((id) => state.leftPanels.includes(id));
  226. state.rightPanels = rightOrder.filter((id) => state.rightPanels.includes(id));
  227. if (state.leftPanels.length === 0) {
  228. state.leftPanels = leftOrder.slice();
  229. }
  230. if (state.rightPanels.length === 0) {
  231. state.rightPanels = rightOrder.slice();
  232. }
  233. state.leftSplitRatio = Number.isFinite(state.leftSplitRatio) ? clamp(state.leftSplitRatio, 0.2, 0.8) : 0.5;
  234. state.rightSplitRatio = Number.isFinite(state.rightSplitRatio) ? clamp(state.rightSplitRatio, 0.2, 0.8) : 0.5;
  235. state.outputDockHeight = Number.isFinite(state.outputDockHeight) ? clamp(state.outputDockHeight, minDockHeight, getDockMaxHeight()) : 220;
  236. state.ruleListHeight = Number.isFinite(state.ruleListHeight) ? clamp(state.ruleListHeight, minRuleListHeight, getRuleListMaxHeight()) : minRuleListHeight;
  237. }
  238. function applyWidths() {
  239. leftContent.style.width = `${state.leftWidth}px`;
  240. rightContent.style.width = `${state.rightWidth}px`;
  241. }
  242. function getDockMaxHeight() {
  243. return Math.max(minDockHeight, Math.floor(window.innerHeight * 0.72));
  244. }
  245. function getRuleListMaxHeight() {
  246. if (!ruleEditorPanel) {
  247. return 1200;
  248. }
  249. const panelHeight = Math.max(0, ruleEditorPanel.clientHeight || 0);
  250. if (panelHeight <= 0) {
  251. return 1200;
  252. }
  253. return Math.max(minRuleListHeight, Math.min(1200, Math.floor(panelHeight * 1.2)));
  254. }
  255. function applyRuleListHeight() {
  256. if (!ruleList) {
  257. return;
  258. }
  259. state.ruleListHeight = clamp(state.ruleListHeight, minRuleListHeight, getRuleListMaxHeight());
  260. ruleList.style.height = `${Math.round(state.ruleListHeight)}px`;
  261. }
  262. function applyBottomDockInset(open) {
  263. const dockOpen = Boolean(open);
  264. const inset = dockOpen ? Math.max(minDockHeight, state.outputDockHeight) : 0;
  265. document.documentElement.style.setProperty("--bottom-dock-space", `${inset}px`);
  266. if (appRoot) {
  267. appRoot.classList.toggle("dock-open", dockOpen);
  268. }
  269. }
  270. function applyBottomDock() {
  271. if (!bottomOutputDock || !outputDockToggle) {
  272. return;
  273. }
  274. const open = Boolean(state.outputDockOpen);
  275. outputDockToggle.classList.toggle("active", open);
  276. bottomOutputDock.classList.toggle("hidden", !open);
  277. applyBottomDockInset(open);
  278. if (!open) {
  279. return;
  280. }
  281. state.outputDockHeight = clamp(state.outputDockHeight, minDockHeight, getDockMaxHeight());
  282. bottomOutputDock.style.height = `${state.outputDockHeight}px`;
  283. applyBottomDockInset(true);
  284. }
  285. function togglePanel(side, panelId) {
  286. if (!panelId) {
  287. return;
  288. }
  289. const key = side === "left" ? "leftPanels" : "rightPanels";
  290. const order = side === "left" ? leftOrder : rightOrder;
  291. const values = state[key].slice();
  292. const index = values.indexOf(panelId);
  293. if (index >= 0) {
  294. values.splice(index, 1);
  295. } else {
  296. values.push(panelId);
  297. }
  298. state[key] = order.filter((id) => values.includes(id));
  299. applySide(side);
  300. updateRuleEditorCompactMode();
  301. requestStageReflow();
  302. saveState();
  303. }
  304. function applySide(side) {
  305. const stack = side === "left" ? leftStack : rightStack;
  306. const windowEl = side === "left" ? leftWindow : rightWindow;
  307. const splitter = side === "left" ? leftSplitter : rightSplitter;
  308. const buttons = side === "left" ? leftButtons : rightButtons;
  309. const panelSplitter = side === "left" ? leftPanelSplitter : rightPanelSplitter;
  310. const key = side === "left" ? "leftPanels" : "rightPanels";
  311. const activePanels = state[key];
  312. const activeSet = new Set(activePanels);
  313. const open = activePanels.length > 0;
  314. windowEl.classList.toggle("collapsed", !open);
  315. splitter.classList.toggle("disabled", !open);
  316. stack.classList.toggle("stack-icon-mode", open);
  317. buttons.forEach((button) => {
  318. const id = button.dataset.panelId || "";
  319. button.classList.toggle("active", activeSet.has(id));
  320. });
  321. const panelList = Array.from(stack.children).filter((node) => node.classList && node.classList.contains("panel"));
  322. panelList.forEach((panel) => {
  323. const isActive = activeSet.has(panel.id);
  324. panel.classList.toggle("panel-slot-hidden", !isActive);
  325. panel.classList.toggle("panel-slot-active", isActive);
  326. panel.classList.remove("top-slot", "bottom-slot");
  327. panel.style.removeProperty("height");
  328. panel.style.removeProperty("order");
  329. });
  330. panelSplitter.classList.add("disabled");
  331. panelSplitter.style.removeProperty("order");
  332. stack.classList.remove("stack-two-open");
  333. if (!open) {
  334. requestStageReflow();
  335. return;
  336. }
  337. if (activePanels.length === 1) {
  338. const panel = document.getElementById(activePanels[0]);
  339. if (panel) {
  340. panel.style.order = "1";
  341. }
  342. return;
  343. }
  344. const topPanel = document.getElementById(activePanels[0]);
  345. const bottomPanel = document.getElementById(activePanels[1]);
  346. if (!topPanel || !bottomPanel) {
  347. return;
  348. }
  349. stack.classList.add("stack-two-open");
  350. panelSplitter.classList.remove("disabled");
  351. topPanel.classList.add("top-slot");
  352. bottomPanel.classList.add("bottom-slot");
  353. topPanel.style.order = "1";
  354. panelSplitter.style.order = "2";
  355. bottomPanel.style.order = "3";
  356. updatePanelHeights(side, false);
  357. }
  358. function updatePanelHeights(side, shouldReflow) {
  359. const stack = side === "left" ? leftStack : rightStack;
  360. const panelSplitter = side === "left" ? leftPanelSplitter : rightPanelSplitter;
  361. const ratioKey = side === "left" ? "leftSplitRatio" : "rightSplitRatio";
  362. if (!stack.classList.contains("stack-two-open")) {
  363. return;
  364. }
  365. const topPanel = stack.querySelector(".panel-slot-active.top-slot");
  366. const bottomPanel = stack.querySelector(".panel-slot-active.bottom-slot");
  367. if (!topPanel || !bottomPanel) {
  368. return;
  369. }
  370. const splitterHeight = panelSplitter.offsetHeight || 8;
  371. const totalHeight = stack.clientHeight;
  372. const available = Math.max(minPanelHeight * 2, totalHeight - splitterHeight);
  373. let ratio = Number(state[ratioKey]);
  374. ratio = Number.isFinite(ratio) ? clamp(ratio, 0.2, 0.8) : 0.5;
  375. let topHeight = Math.round(available * ratio);
  376. topHeight = clamp(topHeight, minPanelHeight, available - minPanelHeight);
  377. const bottomHeight = available - topHeight;
  378. state[ratioKey] = topHeight / available;
  379. topPanel.style.height = `${topHeight}px`;
  380. bottomPanel.style.height = `${bottomHeight}px`;
  381. if (shouldReflow) {
  382. requestStageReflow();
  383. }
  384. }
  385. function startSideDrag(side, event) {
  386. event.preventDefault();
  387. const startX = event.clientX;
  388. const startWidth = side === "left" ? state.leftWidth : state.rightWidth;
  389. document.body.classList.add("layout-resizing");
  390. const onMove = (moveEvent) => {
  391. const delta = moveEvent.clientX - startX;
  392. if (side === "left") {
  393. state.leftWidth = clamp(startWidth + delta, minLeftWidth, maxLeftWidth);
  394. leftContent.style.width = `${state.leftWidth}px`;
  395. } else {
  396. state.rightWidth = clamp(startWidth - delta, minRightWidth, maxRightWidth);
  397. rightContent.style.width = `${state.rightWidth}px`;
  398. }
  399. updateRuleEditorCompactMode();
  400. };
  401. const onUp = () => {
  402. document.body.classList.remove("layout-resizing");
  403. document.removeEventListener("mousemove", onMove);
  404. document.removeEventListener("mouseup", onUp);
  405. saveState();
  406. requestStageReflow();
  407. };
  408. document.addEventListener("mousemove", onMove);
  409. document.addEventListener("mouseup", onUp);
  410. }
  411. function startPanelDrag(side, event) {
  412. event.preventDefault();
  413. const stack = side === "left" ? leftStack : rightStack;
  414. const panelSplitter = side === "left" ? leftPanelSplitter : rightPanelSplitter;
  415. const ratioKey = side === "left" ? "leftSplitRatio" : "rightSplitRatio";
  416. document.body.classList.add("layout-resizing");
  417. const onMove = (moveEvent) => {
  418. const rect = stack.getBoundingClientRect();
  419. const splitterHeight = panelSplitter.offsetHeight || 8;
  420. const available = Math.max(minPanelHeight * 2, rect.height - splitterHeight);
  421. const cursorY = clamp(moveEvent.clientY - rect.top, 0, rect.height);
  422. const ratio = clamp(cursorY / Math.max(1, available), 0.2, 0.8);
  423. state[ratioKey] = ratio;
  424. updatePanelHeights(side, false);
  425. };
  426. const onUp = () => {
  427. document.body.classList.remove("layout-resizing");
  428. document.removeEventListener("mousemove", onMove);
  429. document.removeEventListener("mouseup", onUp);
  430. saveState();
  431. requestStageReflow();
  432. };
  433. document.addEventListener("mousemove", onMove);
  434. document.addEventListener("mouseup", onUp);
  435. }
  436. function startRuleListDrag(event) {
  437. if (!ruleList) {
  438. return;
  439. }
  440. event.preventDefault();
  441. const startY = event.clientY;
  442. const startHeight = ruleList.getBoundingClientRect().height || state.ruleListHeight || minRuleListHeight;
  443. document.body.classList.add("rule-list-resizing");
  444. const onMove = (moveEvent) => {
  445. const delta = moveEvent.clientY - startY;
  446. state.ruleListHeight = clamp(startHeight + delta, minRuleListHeight, getRuleListMaxHeight());
  447. ruleList.style.height = `${Math.round(state.ruleListHeight)}px`;
  448. };
  449. const onUp = () => {
  450. document.body.classList.remove("rule-list-resizing");
  451. document.removeEventListener("mousemove", onMove);
  452. document.removeEventListener("mouseup", onUp);
  453. saveState();
  454. };
  455. document.addEventListener("mousemove", onMove);
  456. document.addEventListener("mouseup", onUp);
  457. }
  458. function startBottomDockDrag(event) {
  459. if (!bottomOutputDock) {
  460. return;
  461. }
  462. event.preventDefault();
  463. const startY = event.clientY;
  464. const startHeight = state.outputDockHeight;
  465. document.body.classList.add("bottom-dock-resizing");
  466. const onMove = (moveEvent) => {
  467. const delta = startY - moveEvent.clientY;
  468. state.outputDockHeight = clamp(startHeight + delta, minDockHeight, getDockMaxHeight());
  469. bottomOutputDock.style.height = `${state.outputDockHeight}px`;
  470. applyBottomDockInset(true);
  471. };
  472. const onUp = () => {
  473. document.body.classList.remove("bottom-dock-resizing");
  474. document.removeEventListener("mousemove", onMove);
  475. document.removeEventListener("mouseup", onUp);
  476. saveState();
  477. requestStageReflow();
  478. };
  479. document.addEventListener("mousemove", onMove);
  480. document.addEventListener("mouseup", onUp);
  481. }
  482. function requestStageReflow() {
  483. window.requestAnimationFrame(() => {
  484. window.dispatchEvent(new Event("resize"));
  485. });
  486. }
  487. })();