@kubohiroya/turbowarp-plus-editor-workbench 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import {createLayout, createSplitter, splitAxis} from './index.ts';
2
2
  import {createTargetList} from './targets.ts';
3
- import type {DrawerViews} from './drawer-views.ts';
4
- import {availableDrawerViews, createDrawerViewBar, drawerPinIcon, hasDrawerBar, readDrawerView, writeDrawerView} from './drawer-views.ts';
3
+ import type {DrawerView, DrawerViews} from './drawer-views.ts';
4
+ import {availableDrawerViews, createDrawerViewBar, drawerLabel, drawerPinIcon, hasDrawerBar, readDrawerView, writeDrawerView} from './drawer-views.ts';
5
5
  import {attachStageDrawerDrag} from './stage-drawer-drag.ts';
6
6
  import type {StageDrawerLayout, StageDrawerPosition} from './workspace-layout.ts';
7
7
 
@@ -15,7 +15,16 @@ const drawerViewWidth = 300;
15
15
  const minimumDrawerViewWidth = 160;
16
16
  const minimumStandardEditor = 380;
17
17
 
18
- export function createStandardLayout(React, {styles, targetStyles, context, LayoutMenu}) {
18
+ type DrawerPanel = (props: {
19
+ id: DrawerView['id'];
20
+ title: string;
21
+ locale: string;
22
+ dockRequest: number;
23
+ renderHeading(undock: import('react').ReactNode): import('react').ReactNode;
24
+ renderContent(detached: boolean): import('react').ReactNode;
25
+ }) => import('react').ReactNode;
26
+
27
+ export function createStandardLayout(React, {styles, targetStyles, context, LayoutMenu, DrawerPanel = undefined as DrawerPanel | undefined}) {
19
28
  const {createElement: element, useContext, useLayoutEffect, useRef, useState} = React;
20
29
  const SplitLayout = createLayout(React);
21
30
  const Splitter = createSplitter(React);
@@ -44,12 +53,17 @@ export function createStandardLayout(React, {styles, targetStyles, context, Layo
44
53
  const hostDrawerViews = availableDrawerViews(drawerViews);
45
54
  // A view saved earlier comes back once the host supplies it again.
46
55
  const drawerView = hostDrawerViews.some(view => view.id === preferredDrawerView) ? preferredDrawerView : 'sprites';
47
- const selectDrawerView = view => {
56
+ const selectDrawerView = React.useCallback(view => {
48
57
  setPreferredDrawerView(view);
49
58
  writeDrawerView(props.storage, props.storageKey, view);
50
- };
59
+ }, [props.storage, props.storageKey]);
51
60
  const body = useRef(null);
52
61
  const plusLayout = enabled && !props.fullscreen;
62
+ const reportDrawerSelection = drawerViews?.onSelectionChange;
63
+ useLayoutEffect(() => {
64
+ reportDrawerSelection?.(plusLayout ? drawerView : 'sprites');
65
+ return () => reportDrawerSelection?.('sprites');
66
+ }, [reportDrawerSelection, plusLayout, drawerView]);
53
67
  const stageHidden = plusLayout && !showStage;
54
68
  const stageDrawerPinned = updateStageDrawer ? stageDrawerLayout?.pinned !== false : localStageDrawerPinned;
55
69
  const setStageDrawerPinned = (pinned: boolean) => {
@@ -398,11 +412,21 @@ export function createStandardLayout(React, {styles, targetStyles, context, Layo
398
412
  function TargetLayout({compact, render}) {
399
413
  const {plusLayout, targetsPinned, locale, drawerViews, drawerView = 'sprites', selectDrawerView} = useContext(context);
400
414
  const tools = useState(null);
415
+ const focusDrawer = useRef(null);
416
+ const [reveal, setReveal] = useState(null as {id: DrawerView['id']; request: number} | null);
417
+ useLayoutEffect(() => {
418
+ if (!plusLayout) return;
419
+ return drawerViews?.registerReveal?.(id => {
420
+ selectDrawerView(id);
421
+ setReveal(previous => ({id, request: (previous?.request ?? 0) + 1}));
422
+ focusDrawer.current?.();
423
+ });
424
+ }, [plusLayout, drawerViews, selectDrawerView]);
401
425
  const panel = render(plusLayout || compact);
402
426
  if (!plusLayout) return panel;
403
427
  const content = element(targetTools.Provider, {value: tools}, panel);
404
428
  const drawer = children => element(EdgeDrawer, {
405
- edge: 'left', pinned: targetsPinned,
429
+ edge: 'left', pinned: targetsPinned, focusPanelRef: focusDrawer,
406
430
  label: isJapanese(locale) ? 'スプライト一覧を展開' : 'Reveal sprite list'
407
431
  }, children);
408
432
  // Without host views the drawer keeps the sprite list as its only, unwrapped content.
@@ -412,14 +436,10 @@ export function createStandardLayout(React, {styles, targetStyles, context, Layo
412
436
  return drawer(element(React.Fragment, null,
413
437
  element(DrawerViewBar, {locale, views, agents: drawerViews.agents, selected: active ? active.id : 'sprites',
414
438
  onSelect: selectDrawerView}),
415
- // Pinning belongs to the drawer, so every view keeps the pin button the sprite list shows.
416
- active && element('div', {className: styles.drawerViewTools},
417
- active.tools?.({locale}),
418
- element(DrawerPinButton, {label: isJapanese(locale) ? 'このパネルを固定表示' : 'Pin this panel'})),
419
439
  // The GUI's target pane stays mounted, so its file inputs and library keep working while hidden.
420
440
  element('div', {className: styles.drawerSprites, hidden: Boolean(active)}, content),
421
- active && element('div', {className: styles.drawerViewContent, 'data-drawer-content': active.id},
422
- active.render({locale}))));
441
+ views.map(view => element(HostDrawerView, {key: view.id, view, locale, active: view === active,
442
+ dockRequest: reveal?.id === view.id ? reveal.request : 0}))));
423
443
  }
424
444
 
425
445
  function SpriteGrid(props) {
@@ -469,16 +489,11 @@ export function createStandardLayout(React, {styles, targetStyles, context, Layo
469
489
  drawerPinIcon(element, targetsPinned));
470
490
  }
471
491
 
472
- function useSpriteListTools() {
473
- const {locale} = useContext(context);
474
- const [mount] = useContext(targetTools) ?? [];
492
+ function useDrawerTools(searchLabel?: string) {
475
493
  const [searching, setSearching] = useState(false);
476
494
  const [query, setQuery] = useState('');
477
495
  const searchButton = useRef(null);
478
496
  const searchInput = useRef(null);
479
- const japanese = isJapanese(locale);
480
- const pinLabel = japanese ? 'スプライト一覧を固定表示' : 'Pin sprite list';
481
- const searchLabel = japanese ? 'スプライトを検索' : 'Search sprites';
482
497
  useLayoutEffect(() => {
483
498
  if (searching) searchInput.current?.focus({preventScroll: true});
484
499
  }, [searching]);
@@ -487,18 +502,20 @@ export function createStandardLayout(React, {styles, targetStyles, context, Layo
487
502
  setQuery('');
488
503
  searchButton.current?.focus();
489
504
  };
490
- const renderTools = openAction => element(React.Fragment, null,
491
- element('div', {className: styles.targetListTools, role: 'group',
492
- 'aria-label': japanese ? 'スプライト一覧の操作' : 'Sprite list tools'},
493
- element('button', {type: 'button', ref: searchButton, 'aria-label': searchLabel, 'aria-expanded': searching,
494
- title: searchLabel, onClick: () => searching ? closeSearch() : setSearching(true)},
495
- element('svg', {'aria-hidden': true, width: 14, height: 14, viewBox: '0 0 16 16',
496
- fill: 'none', stroke: 'currentColor', strokeWidth: 1.4, strokeLinecap: 'round'},
497
- element('circle', {cx: 6.5, cy: 6.5, r: 4}), element('path', {d: 'm9.5 9.5 4 4'}))),
498
- element(DrawerPinButton, {label: pinLabel}),
499
- openAction),
500
- searching && element('input', {type: 'search', className: styles.targetListSearch, 'aria-label': searchLabel,
501
- ref: searchInput, placeholder: japanese ? '名前' : 'Name', value: query,
505
+ const render = ({label, pinLabel, openAction, tools}: {
506
+ label: string; pinLabel?: string; openAction?: import('react').ReactNode; tools?: import('react').ReactNode;
507
+ }) => element(React.Fragment, null,
508
+ element('div', {className: styles.targetListTools, role: 'group', 'aria-label': label},
509
+ tools,
510
+ searchLabel && element('button', {type: 'button', ref: searchButton, 'aria-label': searchLabel,
511
+ 'aria-expanded': searching, title: searchLabel, onClick: () => searching ? closeSearch() : setSearching(true)},
512
+ element('svg', {'aria-hidden': true, width: 14, height: 14, viewBox: '0 0 16 16',
513
+ fill: 'none', stroke: 'currentColor', strokeWidth: 1.4, strokeLinecap: 'round'},
514
+ element('circle', {cx: 6.5, cy: 6.5, r: 4}), element('path', {d: 'm9.5 9.5 4 4'}))),
515
+ pinLabel && element(DrawerPinButton, {label: pinLabel}),
516
+ openAction),
517
+ searching && element('input', {type: 'search', className: styles.targetListSearch,
518
+ 'aria-label': searchLabel, ref: searchInput, placeholder: searchLabel, value: query,
502
519
  onChange: event => setQuery(event.currentTarget.value),
503
520
  onKeyDown: event => {
504
521
  if (event.key === 'Escape') {
@@ -506,8 +523,45 @@ export function createStandardLayout(React, {styles, targetStyles, context, Layo
506
523
  closeSearch();
507
524
  }
508
525
  }}));
526
+ return {query, render};
527
+ }
528
+
529
+ function HostDrawerView({view, locale, active, dockRequest}: {
530
+ view: DrawerView; locale: string; active: boolean; dockRequest: number;
531
+ }) {
532
+ const title = view.label ?? drawerLabel(view.id, locale);
533
+ const japanese = isJapanese(locale);
534
+ const label = japanese ? `${title}の操作` : `${title} tools`;
535
+ const {query, render} = useDrawerTools(view.searchLabel?.(locale));
536
+ const renderHeading = openAction => render({
537
+ label,
538
+ pinLabel: japanese ? 'このパネルを固定表示' : 'Pin this panel',
539
+ openAction,
540
+ tools: view.tools?.({locale, query})
541
+ });
542
+ const renderContent = detached => (active || detached) && element('div', {
543
+ className: styles.drawerViewContent, 'data-drawer-content': view.id
544
+ },
545
+ detached && view.searchLabel && render({label}),
546
+ view.render({locale, query}));
547
+ if (!DrawerPanel && !active) return null;
548
+ return element('div', {className: styles.drawerHostView, hidden: !active}, DrawerPanel ?
549
+ element(DrawerPanel, {id: view.id, title, locale, dockRequest, renderHeading, renderContent}) :
550
+ element(React.Fragment, null, renderHeading(null), renderContent(false)));
551
+ }
552
+
553
+ function useSpriteListTools() {
554
+ const {locale} = useContext(context);
555
+ const [mount] = useContext(targetTools) ?? [];
556
+ const japanese = isJapanese(locale);
557
+ const {query, render} = useDrawerTools(japanese ? 'スプライトを検索' : 'Search sprites');
509
558
  return {
510
- mount, query, renderTools,
559
+ mount, query,
560
+ renderTools: openAction => render({
561
+ label: japanese ? 'スプライト一覧の操作' : 'Sprite list tools',
562
+ pinLabel: japanese ? 'スプライト一覧を固定表示' : 'Pin sprite list',
563
+ openAction
564
+ }),
511
565
  title: japanese ? 'スプライト一覧' : 'Sprite list',
512
566
  openLabel: japanese ? 'アンドック' : 'Undock sprite list'
513
567
  };