@kubohiroya/turbowarp-plus-editor-workbench 0.4.0 → 0.4.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.
@@ -0,0 +1,203 @@
1
+ import type {ReactNode} from 'react';
2
+
3
+ /** The contents the left drawer can show. `sprites` is the GUI's sprite list and backdrop, always available. */
4
+ export type DrawerViewId = 'sprites' | 'project' | 'git-history';
5
+ /** The contents a host supplies. */
6
+ export type HostDrawerViewId = Exclude<DrawerViewId, 'sprites'>;
7
+
8
+ export interface DrawerRenderProps {
9
+ /** The Editor locale, for example `ja` or `en`. */
10
+ locale: string;
11
+ }
12
+
13
+ /** A drawer content supplied by the host. Its button appears only while the host supplies it. */
14
+ export interface DrawerView {
15
+ id: HostDrawerViewId;
16
+ /** The button's name. Defaults to the package's Japanese or English name for `id`. */
17
+ label?: string;
18
+ /** A 16 px inline icon drawn with `currentColor`. Defaults to the package's icon for `id`. */
19
+ icon?: ReactNode;
20
+ /** The content. Rendered only while the view is selected. */
21
+ render(props: DrawerRenderProps): ReactNode;
22
+ /** The view's own tools, shown where the sprite list shows search, pin and undock. */
23
+ tools?(props: DrawerRenderProps): ReactNode;
24
+ }
25
+
26
+ export interface DrawerMenuRenderProps extends DrawerRenderProps {
27
+ /** Close the menu and return focus to its button. */
28
+ close(): void;
29
+ }
30
+
31
+ /** The AI agent menu supplied by the host. Opening it does not change the drawer's content. */
32
+ export interface DrawerMenu {
33
+ label?: string;
34
+ icon?: ReactNode;
35
+ /** The menu content, normally one `role="menu"` element with `menuitem` children. */
36
+ renderMenu(props: DrawerMenuRenderProps): ReactNode;
37
+ }
38
+
39
+ /** What the host passes to `StandardLayout` as `drawerViews`. */
40
+ export interface DrawerViews {
41
+ views?: readonly DrawerView[];
42
+ agents?: DrawerMenu;
43
+ }
44
+
45
+ export const drawerViewIds: readonly DrawerViewId[] = ['sprites', 'project', 'git-history'];
46
+ const hostIds = new Set<string>(drawerViewIds.slice(1));
47
+
48
+ const names = {
49
+ sprites: ['スプライト一覧', 'Sprites'],
50
+ project: ['プロジェクトフォルダ', 'Project folder'],
51
+ 'git-history': ['Git 履歴', 'Git history'],
52
+ agents: ['AI エージェント', 'AI agents'],
53
+ bar: ['ドロワーの表示', 'Drawer views']
54
+ } as const;
55
+
56
+ /** The AI agent button's sparkle, also used beside the host's entry for the AI agent settings. */
57
+ export const agentsIconPath = 'M8 1.5 9.4 6.6 14.5 8 9.4 9.4 8 14.5 6.6 9.4 1.5 8 6.6 6.6Z';
58
+
59
+ export const isJapanese =(locale: string) => /^ja(?:-|$)/i.test(locale ?? '');
60
+ export const drawerLabel = (id: keyof typeof names, locale: string) => names[id][isJapanese(locale) ? 0 : 1];
61
+
62
+ /** The supported host views in button order, without unknown or repeated ids. */
63
+ export function availableDrawerViews(drawerViews: DrawerViews | undefined): DrawerView[] {
64
+ const views = drawerViews?.views ?? [];
65
+ return drawerViewIds.slice(1).flatMap(id => {
66
+ const view = views.find(candidate => candidate && candidate.id === id && hostIds.has(candidate.id) &&
67
+ typeof candidate.render === 'function');
68
+ return view ? [view] : [];
69
+ });
70
+ }
71
+
72
+ /** The bar is shown only when the host supplies something besides the sprite list. */
73
+ export const hasDrawerBar = (drawerViews: DrawerViews | undefined) =>
74
+ availableDrawerViews(drawerViews).length > 0 || typeof drawerViews?.agents?.renderMenu === 'function';
75
+
76
+ type DrawerStorage = Pick<Storage, 'getItem' | 'setItem'>;
77
+ export const drawerViewKey = (storageKey: string) => `${storageKey}.drawer`;
78
+
79
+ /** The view selected last time, or `sprites`. The caller falls back to `sprites` while that view is unavailable. */
80
+ export function readDrawerView(storage: DrawerStorage | null | undefined, storageKey: string | undefined): DrawerViewId {
81
+ if (!storage || !storageKey) return 'sprites';
82
+ try {
83
+ const saved = JSON.parse(storage.getItem(drawerViewKey(storageKey)) ?? 'null');
84
+ if (saved?.version === 1 && drawerViewIds.includes(saved.view)) return saved.view;
85
+ } catch {
86
+ // Storage can be unavailable or contain obsolete data.
87
+ }
88
+ return 'sprites';
89
+ }
90
+
91
+ export function writeDrawerView(storage: DrawerStorage | null | undefined, storageKey: string | undefined, view: DrawerViewId) {
92
+ if (!storage || !storageKey) return;
93
+ try {
94
+ storage.setItem(drawerViewKey(storageKey), JSON.stringify({version: 1, view}));
95
+ } catch {
96
+ // Keep the drawer usable when persistence is denied.
97
+ }
98
+ }
99
+
100
+ let nextMenuId = 0;
101
+
102
+ export function createDrawerViewBar(React, styles) {
103
+ const {createElement: element, useLayoutEffect, useRef, useState} = React;
104
+ const svg = (...children) => element('svg', {
105
+ 'aria-hidden': true, focusable: false, width: 16, height: 16, viewBox: '0 0 16 16',
106
+ fill: 'none', stroke: 'currentColor', strokeWidth: 1.3, strokeLinecap: 'round', strokeLinejoin: 'round'
107
+ }, ...children);
108
+ const icons = {
109
+ sprites: () => svg(element('rect', {x: 2, y: 2, width: 5, height: 5, rx: 1}),
110
+ element('rect', {x: 9, y: 2, width: 5, height: 5, rx: 1}),
111
+ element('rect', {x: 2, y: 9, width: 12, height: 5, rx: 1})),
112
+ project: () => svg(element('path', {d: 'M1.5 4V13h13V5.5H8L6.5 3.5h-4A1 1 0 0 0 1.5 4.5Z'})),
113
+ 'git-history': () => svg(element('circle', {cx: 5, cy: 3.5, r: 1.5}), element('circle', {cx: 5, cy: 12.5, r: 1.5}),
114
+ element('circle', {cx: 11, cy: 6.5, r: 1.5}), element('path', {d: 'M5 5v6M11 8c0 2.5-6 1.5-6 3'})),
115
+ agents: () => svg(element('path', {d: agentsIconPath}))
116
+ };
117
+
118
+ function AgentMenu({menu, locale, button, onFocus, onKeyDown, tabIndex}) {
119
+ const [open, setOpen] = useState(false);
120
+ const popup = useRef(null);
121
+ const id = useRef(null);
122
+ if (!id.current) id.current = `plus-drawer-agents-${++nextMenuId}`;
123
+ const label = menu.label ?? drawerLabel('agents', locale);
124
+ const close = (focus = true) => {
125
+ setOpen(false);
126
+ if (focus) button.current?.focus();
127
+ };
128
+ useLayoutEffect(() => {
129
+ if (!open) return;
130
+ const node = popup.current;
131
+ node?.querySelector('[role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"], button, a[href], input')
132
+ ?.focus({preventScroll: true});
133
+ const doc = node?.ownerDocument;
134
+ const outside = event => {
135
+ if (!node.contains(event.target) && !button.current?.contains(event.target)) close(false);
136
+ };
137
+ doc?.addEventListener('pointerdown', outside, true);
138
+ return () => doc?.removeEventListener('pointerdown', outside, true);
139
+ }, [open]);
140
+ return element('div', {className: styles.drawerAgents},
141
+ element('button', {
142
+ type: 'button', ref: button, tabIndex, 'aria-label': label, title: label,
143
+ 'aria-haspopup': 'menu', 'aria-expanded': open, 'aria-controls': open ? id.current : undefined,
144
+ 'data-drawer-view': 'agents',
145
+ onClick: () => setOpen(value => !value),
146
+ onFocus, onKeyDown
147
+ }, menu.icon ?? icons.agents()),
148
+ open && element('div', {
149
+ id: id.current, ref: popup, className: styles.drawerAgentsMenu,
150
+ onKeyDown: event => {
151
+ if (event.key !== 'Escape') return;
152
+ event.preventDefault();
153
+ event.stopPropagation();
154
+ close();
155
+ }
156
+ }, menu.renderMenu({locale, close: () => close()})));
157
+ }
158
+
159
+ /** The row of icon buttons at the top of the left drawer. */
160
+ return function DrawerViewBar({locale, views, agents, selected, onSelect}) {
161
+ const bar = useRef(null);
162
+ const agentButton = useRef(null);
163
+ const entries = [
164
+ {id: 'sprites', label: drawerLabel('sprites', locale), icon: icons.sprites()},
165
+ ...views.map(view => ({id: view.id, label: view.label ?? drawerLabel(view.id, locale),
166
+ icon: view.icon ?? icons[view.id]()}))
167
+ ];
168
+ const count = entries.length + (agents ? 1 : 0);
169
+ const [focused, setFocused] = useState(() => Math.max(0, entries.findIndex(entry => entry.id === selected)));
170
+ const current = Math.min(focused, count - 1);
171
+ const move = (event, index) => {
172
+ const buttons = bar.current.querySelectorAll('[data-drawer-view]');
173
+ const rtl = bar.current.ownerDocument.defaultView.getComputedStyle(bar.current).direction === 'rtl';
174
+ const step = {ArrowRight: rtl ? -1 : 1, ArrowLeft: rtl ? 1 : -1}[event.key];
175
+ let next;
176
+ if (event.key === 'Home') next = 0;
177
+ else if (event.key === 'End') next = count - 1;
178
+ else if (step) next = (index + step + count) % count;
179
+ else return;
180
+ event.preventDefault();
181
+ setFocused(next);
182
+ buttons[next]?.focus();
183
+ };
184
+ return element('div', {
185
+ className: styles.drawerViewBar, role: 'toolbar', ref: bar,
186
+ 'aria-label': drawerLabel('bar', locale)
187
+ },
188
+ entries.map((entry, index) => element('button', {
189
+ key: entry.id, type: 'button', 'data-drawer-view': entry.id,
190
+ 'aria-label': entry.label, title: entry.label, 'aria-pressed': entry.id === selected,
191
+ tabIndex: index === current ? 0 : -1,
192
+ onFocus: () => setFocused(index),
193
+ onClick: () => onSelect(entry.id),
194
+ onKeyDown: event => move(event, index)
195
+ }, entry.icon)),
196
+ agents && element(AgentMenu, {
197
+ menu: agents, locale, button: agentButton,
198
+ tabIndex: current === entries.length ? 0 : -1,
199
+ onFocus: () => setFocused(entries.length),
200
+ onKeyDown: event => move(event, entries.length)
201
+ }));
202
+ };
203
+ }
package/src/editor.css CHANGED
@@ -631,6 +631,276 @@ details.layout-menu button {
631
631
  font: inherit;
632
632
  }
633
633
 
634
+ .drawer-view-bar {
635
+ position: relative;
636
+ display: grid;
637
+ flex: none;
638
+ /* One row as long as the drawer is wide enough; the buttons wrap only when it is too narrow. */
639
+ grid-template-columns: repeat(auto-fit, minmax(1.375rem, 1fr));
640
+ gap: 0.125rem;
641
+ padding-block-end: 0.25rem;
642
+ margin-block-end: 0.25rem;
643
+ border-block-end: 1px solid var(--ui-black-transparent);
644
+ }
645
+
646
+ .drawer-view-bar button {
647
+ display: grid;
648
+ place-items: center;
649
+ inline-size: 100%;
650
+ min-block-size: 1.75rem;
651
+ padding: 0.125rem;
652
+ border: 1px solid transparent;
653
+ border-radius: 0.25rem;
654
+ background: transparent;
655
+ color: var(--text-primary);
656
+ cursor: pointer;
657
+ }
658
+
659
+ .drawer-view-bar button:hover,
660
+ .drawer-view-bar button:focus-visible {
661
+ border-color: var(--ui-black-transparent);
662
+ background: var(--ui-secondary);
663
+ }
664
+
665
+ .drawer-view-bar button[aria-pressed="true"],
666
+ .drawer-view-bar button[aria-expanded="true"] {
667
+ border-color: var(--looks-secondary, #855cd6);
668
+ background: var(--ui-secondary);
669
+ color: var(--looks-secondary, #855cd6);
670
+ }
671
+
672
+ .drawer-view-bar svg {
673
+ inline-size: 1rem;
674
+ block-size: 1rem;
675
+ }
676
+
677
+ .drawer-agents {
678
+ display: contents;
679
+ }
680
+
681
+ .drawer-agents-menu {
682
+ position: absolute;
683
+ inset-block-start: 100%;
684
+ inset-inline-start: 0;
685
+ z-index: 120;
686
+ min-inline-size: 12rem;
687
+ padding: 0.25rem;
688
+ border: 1px solid var(--ui-black-transparent);
689
+ border-radius: 0.25rem;
690
+ background: var(--ui-white, #fff);
691
+ color: var(--text-primary);
692
+ box-shadow: 0 0.125rem 0.5rem var(--ui-black-transparent);
693
+ }
694
+
695
+ .ai-agent-menu {
696
+ display: flex;
697
+ flex-direction: column;
698
+ }
699
+
700
+ /* Two classes, so the drawer bar's own button rules do not reach the menu items. */
701
+ .ai-agent-menu > .ai-agent-menu-item {
702
+ display: flex;
703
+ align-items: center;
704
+ gap: 0.5rem;
705
+ inline-size: 100%;
706
+ padding: 0.375rem 0.5rem;
707
+ border: 0;
708
+ border-radius: 0.25rem;
709
+ background: transparent;
710
+ color: inherit;
711
+ font: inherit;
712
+ font-size: 0.875rem;
713
+ white-space: nowrap;
714
+ text-align: start;
715
+ cursor: pointer;
716
+ }
717
+
718
+ .ai-agent-menu > .ai-agent-menu-item:hover,
719
+ .ai-agent-menu > .ai-agent-menu-item:focus-visible {
720
+ border-color: transparent;
721
+ background: var(--ui-secondary);
722
+ color: inherit;
723
+ outline: none;
724
+ }
725
+
726
+ .ai-agent-menu-item svg {
727
+ flex: none;
728
+ inline-size: 1rem;
729
+ block-size: 1rem;
730
+ }
731
+
732
+ .ai-agent-menu-separator {
733
+ margin-block: 0.25rem;
734
+ border-block-start: 1px solid var(--ui-black-transparent);
735
+ }
736
+
737
+ .ai-agent-menu-hint {
738
+ max-inline-size: 16rem;
739
+ margin: 0.25rem 0.5rem 0.5rem;
740
+ font-size: 0.75rem;
741
+ line-height: 1.4;
742
+ }
743
+
744
+ .ai-agent-dialog-backdrop {
745
+ position: fixed;
746
+ inset: 0;
747
+ z-index: 1000;
748
+ display: grid;
749
+ place-items: center;
750
+ padding: 1rem;
751
+ background: rgb(0 0 0 / 40%);
752
+ }
753
+
754
+ .ai-agent-dialog {
755
+ box-sizing: border-box;
756
+ inline-size: min(36rem, 100%);
757
+ max-block-size: calc(100vh - 2rem);
758
+ overflow: auto;
759
+ padding: 1rem 1.25rem;
760
+ border: 1px solid var(--ui-black-transparent);
761
+ border-radius: 0.5rem;
762
+ background: var(--ui-white, #fff);
763
+ color: var(--text-primary);
764
+ box-shadow: 0 0.25rem 1.5rem var(--ui-black-transparent);
765
+ font-size: 0.875rem;
766
+ }
767
+
768
+ .ai-agent-dialog h2 {
769
+ margin: 0 0 0.25rem;
770
+ font-size: 1.125rem;
771
+ }
772
+
773
+ .ai-agent-dialog > p {
774
+ margin: 0 0 0.75rem;
775
+ line-height: 1.5;
776
+ }
777
+
778
+ .ai-agent-dialog-section {
779
+ margin: 0 0 0.75rem;
780
+ padding: 0.5rem 0.75rem 0.75rem;
781
+ border: 1px solid var(--ui-black-transparent);
782
+ border-radius: 0.375rem;
783
+ }
784
+
785
+ .ai-agent-dialog-section > legend {
786
+ padding-inline: 0.25rem;
787
+ font-weight: 700;
788
+ }
789
+
790
+ .ai-agent-dialog-list {
791
+ display: flex;
792
+ flex-direction: column;
793
+ gap: 0.5rem;
794
+ margin-block-end: 0.5rem;
795
+ }
796
+
797
+ .ai-agent-dialog-row {
798
+ display: flex;
799
+ flex-wrap: wrap;
800
+ align-items: flex-start;
801
+ gap: 0.25rem 0.75rem;
802
+ padding-block: 0.375rem;
803
+ }
804
+
805
+ .ai-agent-dialog-row + .ai-agent-dialog-row {
806
+ border-block-start: 1px solid var(--ui-black-transparent);
807
+ }
808
+
809
+ .ai-agent-dialog-field {
810
+ display: flex;
811
+ flex: 1 1 12rem;
812
+ flex-direction: column;
813
+ gap: 0.125rem;
814
+ }
815
+
816
+ .ai-agent-dialog-field input {
817
+ box-sizing: border-box;
818
+ inline-size: 100%;
819
+ padding: 0.375rem 0.5rem;
820
+ border: 1px solid var(--ui-black-transparent);
821
+ border-radius: 0.25rem;
822
+ background: var(--ui-primary, #fff);
823
+ color: inherit;
824
+ font: inherit;
825
+ }
826
+
827
+ .ai-agent-dialog-field input[aria-invalid="true"] {
828
+ border-color: #d33;
829
+ }
830
+
831
+ .ai-agent-dialog-check {
832
+ display: flex;
833
+ flex-wrap: wrap;
834
+ align-items: center;
835
+ gap: 0.375rem;
836
+ flex-basis: 100%;
837
+ }
838
+
839
+ .ai-agent-dialog-error {
840
+ flex-basis: 100%;
841
+ margin: 0;
842
+ color: #c22;
843
+ font-size: 0.75rem;
844
+ }
845
+
846
+ .ai-agent-dialog-note {
847
+ flex-basis: 100%;
848
+ margin: 0;
849
+ font-size: 0.75rem;
850
+ opacity: 0.8;
851
+ }
852
+
853
+ .ai-agent-dialog-actions {
854
+ display: flex;
855
+ justify-content: flex-end;
856
+ gap: 0.5rem;
857
+ }
858
+
859
+ .ai-agent-dialog-button {
860
+ padding: 0.375rem 0.75rem;
861
+ border: 1px solid var(--ui-black-transparent);
862
+ border-radius: 0.25rem;
863
+ background: var(--ui-secondary, #f3f3f6);
864
+ color: inherit;
865
+ font: inherit;
866
+ cursor: pointer;
867
+ }
868
+
869
+ .ai-agent-dialog-row > .ai-agent-dialog-button {
870
+ align-self: center;
871
+ }
872
+
873
+ .ai-agent-dialog-primary {
874
+ border-color: var(--looks-secondary, #855cd6);
875
+ background: var(--looks-secondary, #855cd6);
876
+ color: #fff;
877
+ }
878
+
879
+ .drawer-view-tools {
880
+ display: flex;
881
+ flex: none;
882
+ align-items: center;
883
+ justify-content: space-evenly;
884
+ padding-block: 0.125rem;
885
+ }
886
+
887
+ .drawer-sprites {
888
+ display: contents;
889
+ }
890
+
891
+ .drawer-sprites[hidden] {
892
+ display: none;
893
+ }
894
+
895
+ .drawer-view-content {
896
+ display: flex;
897
+ flex: 1;
898
+ flex-direction: column;
899
+ min-block-size: 0;
900
+ min-inline-size: 0;
901
+ overflow: auto;
902
+ }
903
+
634
904
  .drawer-pin {
635
905
  flex: none;
636
906
  opacity: 0.55;
@@ -90,3 +90,40 @@
90
90
  flex-direction: row;
91
91
  justify-content: flex-end;
92
92
  }
93
+ .tw-plus-target-menu-icon {
94
+ width: 1em;
95
+ height: 1em;
96
+ margin-inline-end: 0.45em;
97
+ vertical-align: -0.125em;
98
+ fill: none;
99
+ stroke: currentColor;
100
+ stroke-width: 1.7;
101
+ stroke-linecap: round;
102
+ stroke-linejoin: round;
103
+ pointer-events: none;
104
+ }
105
+
106
+ .tw-plus-target-menu-button {
107
+ width: 100%;
108
+ border: 0;
109
+ background: transparent;
110
+ color: inherit;
111
+ font: inherit;
112
+ text-align: start;
113
+ }
114
+
115
+ .tw-plus-target-submenu-arrow {
116
+ float: inline-end;
117
+ margin-inline-start: 1em;
118
+ }
119
+
120
+ .tw-plus-target-submenu {
121
+ visibility: hidden;
122
+ opacity: 0;
123
+ }
124
+
125
+ .tw-plus-target-submenu.react-contextmenu--visible {
126
+ visibility: visible;
127
+ opacity: 1;
128
+ pointer-events: auto;
129
+ }