WebIDE/src/plugins/alex-app.plugin.ts

51 lines
1.4 KiB
TypeScript

import type { IPluginAPI } from "./common";
export const PLUGIN_ID = "alex-app";
let handleHashChange: (this: Window, ev: HashChangeEvent) => any;
export const activate = ({ commands }: IPluginAPI) => {
let ignoreHash: string | null = null;
commands.registerCommand(
"code-service.replace-browser-url",
(path: string) => {
const fullPath = `/gitlink${path}`;
console.log(fullPath);
const [owner, name, blob, ref, ...filePath] = path.split('/')
window.history.replaceState(null, "", fullPath);
}
);
// scm 插件使用 英文 en-us
// TODO alex内暴露命令
commands.registerCommand(
'alex.env.language',
() => {
return 'zh-cn'
}
),
commands.registerCommand(
"code-service.replace-browser-url-hash",
(hash: string) => {
if (hash !== location.hash) {
ignoreHash = hash;
const { href } = window.location;
const hashIndex = href.indexOf("#");
const url = hashIndex === -1 ? href : href.slice(0, hashIndex);
window.location.replace(`${url}${hash}`);
}
}
);
handleHashChange = () => {
const { hash } = location;
if (ignoreHash === hash) return;
ignoreHash = null;
commands.executeCommand("code-service.set-line-hash", hash);
};
window.addEventListener("hashchange", handleHashChange);
};
export const deactivate = () => {
window.removeEventListener("hashchange", handleHashChange);
};