Update editor, workspace, Git services, appearance, and project guides
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
import { mount, unmount } from 'svelte';
|
||||
import { get } from 'svelte/store';
|
||||
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
|
||||
import type { PluginApi, PluginViewOpenOptions } from '$lib/plugins/sdk';
|
||||
import Browser from './Browser.svelte';
|
||||
import Settings from './Settings.svelte';
|
||||
import Bookmarks from './Bookmarks.svelte';
|
||||
import { connect, request, state } from './client';
|
||||
import type { TabStatus } from './tabSession';
|
||||
|
||||
type BrowserControl = { find(): Promise<void>; detach(): void; action(command: string, args?: Record<string, unknown>): Promise<void> };
|
||||
type SavedView = { view: string; profileId?: string; url?: string; runtimeId: string; status?: TabStatus; control?: BrowserControl };
|
||||
|
||||
export async function activate(api: PluginApi) {
|
||||
const dispose = await connect(api.signal); api.onDispose(dispose);
|
||||
const owners = new Map<string, SavedView>();
|
||||
const offMove = await getCurrentWebviewWindow().listen<{ tabId: string }>('browser-tab-moved', ({ payload }) => {
|
||||
const saved = owners.get(payload.tabId);
|
||||
if (!saved) return;
|
||||
saved.control?.detach();
|
||||
api.closeView?.(saved.view);
|
||||
});
|
||||
api.onDispose(offMove);
|
||||
const off = state.subscribe(value => {
|
||||
for (const tab of value?.tabs || []) {
|
||||
const saved = owners.get(tab.id);
|
||||
if (saved) {
|
||||
saved.profileId = tab.profileId; if (tab.url) saved.url = tab.url;
|
||||
api.updateViewTitle?.(saved.view, `${tab.title || 'Browser'} · ${value!.profiles.find(p => p.id === tab.profileId)?.name || 'User'}`.slice(0, 200));
|
||||
api.updateViewIcon?.(saved.view, tab.favicon ?? null);
|
||||
}
|
||||
}
|
||||
});
|
||||
api.onDispose(off);
|
||||
const mounts = new Set<() => Promise<void>>();
|
||||
function retain(instance: ReturnType<typeof mount>) {
|
||||
let result: Promise<void> | undefined;
|
||||
const cleanup = () => result ??= unmount(instance).then(() => { mounts.delete(cleanup); });
|
||||
mounts.add(cleanup); return cleanup;
|
||||
}
|
||||
api.onDispose(async () => { await Promise.all([...mounts].map(cleanup => cleanup())); });
|
||||
async function open(profileId?: string, url?: string, options?: PluginViewOpenOptions, tabId?: string, pinned = false) {
|
||||
const id = `tab-${crypto.randomUUID()}`;
|
||||
const saved: SavedView = { view: id, profileId, url, runtimeId: '' };
|
||||
const run = (command: string, args?: Record<string, unknown>) => { void saved.control?.action(command, args); };
|
||||
const split = (direction: 'right' | 'down') => {
|
||||
const current = get(state)?.tabs.find(tab => tab.id === saved.runtimeId);
|
||||
void open(current?.profileId || saved.profileId, current?.url || saved.url || 'about:blank', { split: direction, relativeTo: id });
|
||||
};
|
||||
api.registerView({ id, title: 'Browser', placement: 'editor', icon: 'globe', pinned, launcher: false,
|
||||
tabDragData: ({ pinned }) => {
|
||||
const current = get(state)?.tabs.find(tab => tab.id === saved.runtimeId);
|
||||
if (saved.status?.phase !== 'ready' || !current?.transferToken) return undefined;
|
||||
return JSON.stringify({ tabId: current.id, transferToken: current.transferToken, pinned });
|
||||
},
|
||||
find: () => { api.openView(id); void saved.control?.find(); },
|
||||
tabActions: ({ pinned }) => {
|
||||
const current = get(state)?.tabs.find(tab => tab.id === saved.runtimeId);
|
||||
const ready = saved.status?.phase === 'ready';
|
||||
const web = ready && /^https?:\/\//.test(current?.url || '');
|
||||
return [
|
||||
{ separator: true },
|
||||
{ label: 'Move to new window', icon: 'external', disabled: !ready, run: () => run('moveToWindow', { pinned }) },
|
||||
{ label: 'Split right', icon: 'split-right', disabled: !ready, run: () => split('right') },
|
||||
{ label: 'Split down', icon: 'split-down', disabled: !ready, run: () => split('down') },
|
||||
{ separator: true },
|
||||
{ label: 'Find…', icon: 'search', shortcut: 'Ctrl+F', disabled: !ready, run: () => { api.openView(id); void saved.control?.find(); } },
|
||||
{ label: 'Open in default browser', icon: 'external', disabled: !web, run: () => run('openExternal') },
|
||||
{ label: 'Bookmark', icon: 'bookmark', disabled: !web, run: () => run('bookmark') },
|
||||
];
|
||||
}, mount: container => {
|
||||
let runtimeId = '';
|
||||
const instance = mount(Browser, { target: container, props: {
|
||||
initial: { tabId, profileId: saved.profileId, url: saved.url },
|
||||
onSessionChange: value => {
|
||||
saved.status = value;
|
||||
if (value.tabId !== runtimeId || value.phase === 'closed') {
|
||||
owners.delete(runtimeId);
|
||||
runtimeId = value.phase === 'closed' ? '' : value.tabId;
|
||||
if (runtimeId) owners.set(runtimeId, saved);
|
||||
}
|
||||
saved.runtimeId = runtimeId;
|
||||
api.updateViewIcon?.(saved.view, get(state)?.tabs.find(tab => tab.id === runtimeId)?.favicon ?? null);
|
||||
if (value.profileId) saved.profileId = value.profileId;
|
||||
if (value.url) saved.url = value.url;
|
||||
},
|
||||
newTab: (profile: string) => open(profile),
|
||||
} });
|
||||
tabId = undefined;
|
||||
saved.control = instance;
|
||||
const cleanup = retain(instance);
|
||||
return async () => { await cleanup(); if (saved.control === instance) saved.control = undefined; };
|
||||
} });
|
||||
api.openView(id, options);
|
||||
}
|
||||
api.registerCommand({ id: 'new-tab', title: 'New browser tab', category: 'open', run: () => open() });
|
||||
api.contribute('browser', {
|
||||
newTab: () => open(),
|
||||
mountSettings: container => retain(mount(Settings, { target: container })),
|
||||
mountBookmarks: container => retain(mount(Bookmarks, { target: container, props: { open } })),
|
||||
open: async url => {
|
||||
const value = await request('state');
|
||||
if (value.settings.linkDestination === 'external') return false;
|
||||
await open(undefined, url); return true;
|
||||
},
|
||||
});
|
||||
api.registerTabDrop?.(async data => {
|
||||
const payload = JSON.parse(data);
|
||||
if (!payload || typeof payload.tabId !== 'string' || typeof payload.transferToken !== 'string') throw new Error('Invalid browser tab transfer.');
|
||||
const existing = owners.get(payload.tabId);
|
||||
if (existing) { api.openView(existing.view); return; }
|
||||
const snapshot = await request('adoptTab', { tabId: payload.tabId, transferToken: payload.transferToken });
|
||||
const current = snapshot.tabs.find(tab => tab.id === payload.tabId);
|
||||
if (!current) throw new Error('The browser tab is unavailable.');
|
||||
await open(current.profileId, current.url, undefined, current.id, payload.pinned === true);
|
||||
});
|
||||
const incoming = new URLSearchParams(location.search).get('browserTab');
|
||||
if (incoming) {
|
||||
const snapshot = await request('adoptTab', { tabId: incoming });
|
||||
const current = snapshot.tabs.find(tab => tab.id === incoming);
|
||||
await open(current?.profileId, current?.url, undefined, incoming, new URLSearchParams(location.search).get('browserPinned') === 'true');
|
||||
const url = new URL(location.href);
|
||||
url.searchParams.delete('browserTab'); url.searchParams.delete('browserPinned');
|
||||
history.replaceState(null, '', url.pathname + url.search);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user