Skip to content

Commit

Permalink
feat(vscode): 侧边栏状态保持 (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
aooiuu committed Jul 19, 2024
1 parent 1f471da commit 7584fbc
Show file tree
Hide file tree
Showing 31 changed files with 337 additions and 284 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ jobs:
- run: pnpm install
- run: pnpm run build
- run: pnpm run vscode:build-tpl
- name: rename vscode vsx
# working-directory 用于指定在哪个目录下执行命令
working-directory: ./packages/vscode
run: pnpm run build:rename

# https://github.com/HaaLeo/publish-vscode-extension
- name: build vscode vsx
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ jobs:
- run: pnpm install
- run: pnpm run build
- run: pnpm run vscode:build-tpl
- name: rename vscode vsx
# working-directory 用于指定在哪个目录下执行命令
working-directory: ./packages/vscode
run: pnpm run build:rename

# https://github.com/HaaLeo/publish-vscode-extension
- name: build vscode vsx
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ jobs:
if: matrix.os == 'windows-latest'
run: pnpm run vscode:build-tpl

- name: rename vscode vsx
if: matrix.os == 'windows-latest'
# working-directory 用于指定在哪个目录下执行命令
working-directory: ./packages/vscode
run: pnpm run build:rename

# https://github.com/HaaLeo/publish-vscode-extension
- name: build vscode vsx
if: matrix.os == 'windows-latest'
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"cross-env": "^7.0.3",
"husky": "^8.0.0",
"npm-run-all": "^4.1.5",
"tsup": "^8.1.2",
"tsx": "^4.16.2",
"unbuild": "^2.0.0"
},
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"lib": ["esnext", "DOM"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"skipLibCheck": true
}
Expand Down
2 changes: 0 additions & 2 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"lib": ["esnext", "DOM"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"skipLibCheck": true
}
Expand Down
2 changes: 0 additions & 2 deletions packages/epub/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"lib": ["esnext", "DOM"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"paths": {}
Expand Down
2 changes: 0 additions & 2 deletions packages/rule-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"lib": ["esnext", "DOM"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"skipLibCheck": true
}
Expand Down
2 changes: 0 additions & 2 deletions packages/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"lib": ["esnext", "DOM"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"skipLibCheck": true
}
Expand Down
6 changes: 0 additions & 6 deletions packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
// https://github.com/typeorm/typeorm
"emitDecoratorMetadata": true,
"experimentalDecorators": true,

"target": "es2018",
"module": "esnext",
"lib": ["esnext"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"skipLibCheck": true
}
Expand Down
2 changes: 0 additions & 2 deletions packages/utools/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"lib": ["esnext", "DOM"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true,
"skipLibCheck": true
}
Expand Down
21 changes: 2 additions & 19 deletions packages/vscode/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,9 @@
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}"
],
"args": ["--disable-extensions", "--extensionDevelopmentPath=${workspaceFolder}"],
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "${defaultBuildTask}"
},
{
"name": "Extension Tests",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js",
"${workspaceFolder}/dist/**/*.js"
],
"preLaunchTask": "tasks: watch-tests"
"preLaunchTask": "npm: dev"
}
]
}
35 changes: 12 additions & 23 deletions packages/vscode/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,22 @@
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": ["$ts-webpack-watch", "$tslint-webpack-watch"],
"script": "dev",
"isBackground": true,
"presentation": {
"reveal": "never",
"group": "watchers"
},
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "npm",
"script": "watch-tests",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never",
"group": "watchers"
"reveal": "never"
},
"problemMatcher": [
{
"base": "$ts-webpack-watch",
"background": {
"activeOnStart": true,
"beginsPattern": "Build start",
"endsPattern": "Build success"
}
}
],
"group": "build"
},
{
"label": "tasks: watch-tests",
"dependsOn": ["npm: watch", "npm: watch-tests"],
"problemMatcher": []
}
]
}
1 change: 1 addition & 0 deletions packages/vscode/.vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ vsc-extension-quickstart.md
**/.eslintrc.json
**/*.map
**/*.ts
**/*.cjs
16 changes: 4 additions & 12 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,10 @@
],
"main": "./dist/extension.js",
"scripts": {
"build:rename": "tsx ./scripts/publish.cjs",
"build:vsce": "vsce package --no-dependencies",
"build": "npm-run-all build:rename build:vsce",
"vscode:prepublish": "pnpm run package",
"compile": "webpack",
"watch": "webpack --watch",
"package": "webpack --mode production --devtool hidden-source-map",
"compile-tests": "tsc -p . --outDir out",
"watch-tests": "tsc -p . -w --outDir out",
"pretest": "pnpm run compile-tests && pnpm run compile && pnpm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
"build": "tsup",
"dev": "tsx ./scripts/publish.cjs && tsup --watch src",
"vscode:prepublish": "tsx ./scripts/publish.cjs",
"pack": "vsce package --no-dependencies"
},
"contributes": {
"commands": [
Expand Down
26 changes: 23 additions & 3 deletions packages/vscode/scripts/publish.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
const { dirname, join } = require('path');
const fs = require('fs-extra');
const path = require('path');

const root = dirname(__dirname);

function parseVersion(version) {
const match = version.match(/^v?(\d{1,5})\.(\d{1,5})\.(\d{1,5})(?:-([0-9A-Za-z]+))?(?:\.([0-9A-Za-z-.]+))?$/);
if (!match) {
throw new Error('Unable to parse: ' + version);
}
var res = {
major: parseInt(match[1], 10),
minor: parseInt(match[2], 10),
patch: parseInt(match[3], 10),
pre: match[4] || '',
build: parseInt(match[5], 10)
};

return res;
}

async function publish() {
const pkgPath = join(root, 'package.json');
const rawJSON = await fs.readFile(pkgPath, 'utf-8');
Expand All @@ -11,10 +28,13 @@ async function publish() {

if (pkg.version.includes('-')) {
// 非正式版
const version = pkg.version.replace(/\-.*?$/, '');
const patch = 10000 + version.split('.').pop();
pkg.version = pkg.version.replace(/\.\d+\-.*?$/, '.' + patch);
const version = parseVersion(pkg.version);
pkg.version = pkg.version.replace(/\.\d+\-.*?$/, '.' + (10000 + version.build));
}

// sql-wasm.wasm
fs.copyFileSync(path.resolve(root, '../../node_modules/sql.js/dist/sql-wasm.wasm'), path.join(root, 'dist', 'sql-wasm.wasm'));

pkg.devDependencies = {};
await fs.writeJSON(pkgPath, pkg, { spaces: 2 });
}
Expand Down
10 changes: 1 addition & 9 deletions packages/vscode/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { stringify } from 'qs';
import { ensureDirSync } from 'fs-extra';
import { openExplorer } from 'explorer-opener';
import { CONSTANTS } from '@any-reader/shared';
import { COMMANDS, BOOK_SOURCE_PATH } from './constants';
import { COMMANDS } from './constants';
import { webviewProvider } from './sidebar/webviewProvider';
import { WebView } from './webview';
import { getConfig } from './utils/config';
Expand All @@ -18,7 +18,6 @@ class App {
// 注册命令
const registerCommand = vscode.commands.registerCommand;
[
registerCommand(COMMANDS.editBookSource, this.editBookSource, this),
registerCommand(COMMANDS.searchBook, this.searchBook, this),
registerCommand(COMMANDS.getChapter, this.getChapter, this),
registerCommand(COMMANDS.discover, this.discover, this),
Expand All @@ -34,13 +33,6 @@ class App {
vscode.window.registerWebviewViewProvider('any-reader-webview', webviewProvider);
}

// 书源编辑
editBookSource() {
vscode.workspace.openTextDocument(vscode.Uri.file(BOOK_SOURCE_PATH)).then((doc) => {
vscode.window.showTextDocument(doc);
});
}

// 搜索
async searchBook() {
this.webView.navigateTo('/search');
Expand Down
17 changes: 0 additions & 17 deletions packages/vscode/src/config.ts

This file was deleted.

9 changes: 1 addition & 8 deletions packages/vscode/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as os from 'os';
import * as path from 'path';

export enum COMMANDS {
editBookSource = 'any-reader.editBookSource',
searchBook = 'any-reader.search',
getChapter = 'any-reader.getChapter',
discover = 'any-reader.discover',
Expand All @@ -13,10 +12,4 @@ export enum COMMANDS {
}

export const ROOT_PATH = path.join(os.homedir(), '.any-reader');

// 规则路径
export const BOOK_SOURCE_PATH = path.join(ROOT_PATH, 'book-source.json');
// 历史记录路径
export const HISTORY_PATH = path.join(ROOT_PATH, 'history.json');
// 收藏配置路径
export const FAVORITES_PATH = path.join(ROOT_PATH, 'favorites.json');
export const CONFIG_PATH = path.join(ROOT_PATH, 'config.vscode.json');
2 changes: 2 additions & 0 deletions packages/vscode/src/editorProvider/CustomEditorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ export class CustomEditorProvider implements vscode.CustomReadonlyEditorProvider
this.webviewPanel.webview.options = {
enableScripts: true
};

// 绑定事件
useWebviewEvent(this.webviewPanel.webview, this._context.extensionPath);

// 打开模板
this.navigateTo(
'/chapter?' +
Expand Down
15 changes: 15 additions & 0 deletions packages/vscode/src/sidebar/webviewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class WebviewProvider implements vscode.WebviewViewProvider {
private _webviewView!: vscode.WebviewView;
private _extensionPath!: string;
private _event!: WebviewEvent;
private route?: string;

setExtensionPath(extensionPath: string) {
this._extensionPath = extensionPath;
Expand All @@ -17,8 +18,22 @@ export class WebviewProvider implements vscode.WebviewViewProvider {
this.webview.options = {
enableScripts: true
};

this.webview.html = getWebViewContent(path.join('template-dist', 'index.html'), this._extensionPath, this.webview);
this._event = useWebviewEvent(this.webview, this._extensionPath);

// 保存上下文
this._event.pm.answer('post@vscode/saveRoute', async (data) => {
this.route = data.fullPath;
return true;
});

webviewView.onDidChangeVisibility(() => {
if (webviewView.visible && this.route) {
let injectScript = `window.__vscode$initialize_page = '${this.route}';`;
this.webview.html = getWebViewContent(path.join('template-dist', 'index.html'), this._extensionPath, this.webview, injectScript);
}
});
}

get webviewView() {
Expand Down
4 changes: 1 addition & 3 deletions packages/vscode/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as os from 'node:os';
import * as path from 'node:path';
import * as fs from 'fs-extra';

export const ROOT_PATH = path.join(os.homedir(), '.any-reader');
export const CONFIG_PATH = path.join(ROOT_PATH, 'config.vscode.json');
import { CONFIG_PATH } from '../constants';

fs.ensureFileSync(CONFIG_PATH);

Expand Down
Loading

0 comments on commit 7584fbc

Please sign in to comment.