前期准备
安装相关依赖 npm install -g yo generator-code
运行 yo code
创建项目,根据喜好配置文件,我这里选择JavaScript,不过我更建议使用TypeScript
开发
创建一个电子书工具类
const vscode = require('vscode');
class Book {
/**
* @param {vscode.ExtensionContext} extensionContext
*/
constructor(extensionContext) {
this.extensionContext = extensionContext; //上下文
this.curr_page_number = 0; //当前页
this.page = 0; //总页数
}
/**
* @param {string[]} text
*/
getSize(text) { //获取总页数
console.log("text", text)
this.page = text.length - 1;
}
/**
* @param {string} type
*/
getPage(type) { //获取当前页数,workspaceState为当前工作空间保存,globalState全局保存。
//根据type进行更新
let curr_page = this.extensionContext.workspaceState.get('novelExt.currPageNumber');
if (curr_page === undefined) { curr_page = 0; }
let page = 0;
if (type === "previous") {
if (curr_page <= 0) {
page = 0;
} else {
page = curr_page - 1;
}
} else if (type === "next") {
if (curr_page >= this.page) {
page = this.page;
} else {
page = curr_page + 1;
}
} else if (type === "curr") {
page = curr_page;
}
this.curr_page_number = page;
// this.curr_page_number = this.extensionContext.globalState.get("book_page_number", 1);
}
updatePage() {//更新当前页
this.extensionContext.workspaceState.update('novelExt.currPageNumber', this.curr_page_number);
console.log("this.curr_page_number", this.curr_page_number);
// this.extensionContext.globalState.update("book_page_number", page);
}
//读取全局保存的novelExt.text关键词对应的值
readFile() {
const data = this.extensionContext.globalState.get('novelExt.text');
// console.log(data);
return data;
}
// 获取上一页
getPreviousPage() {
const text = this.readFile();
this.getSize(text);
this.getPage("previous");
this.updatePage();
return text[this.curr_page_number];
}
//获取下一页
getNextPage() {
const text = this.readFile();
this.getSize(text);
this.getPage("next");
this.updatePage();
return text[this.curr_page_number];
}
//获取当前存储的页
getJumpingPage() {
const text = this.readFile();
this.getSize(text);
this.getPage("curr");
this.updatePage();
return text[this.curr_page_number];
}
}
module.exports = Book;
插件主体开发
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const Book = require('./bookUtil');
const superagent = require('superagent');
const QueryClass = require('./querClass');
const cheerio = require('cheerio');
const fs = require('fs');
const epub = require('epub');
// 状态栏
let nextBar = null
let prevBar = null
let hideBar = null
let jumpBar = null
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function init() {
// 上一页的bar
prevBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
prevBar.text = "上一页";
prevBar.command = 'novelExt.getPreviousPage'
// 下一页的bar
nextBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
nextBar.text = "下一页";
nextBar.command = 'novelExt.getNextPage'
// 老板键的bar
hideBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
hideBar.text = "隐藏";
hideBar.command = 'novelExt.displayCode'
// 展示的bar
jumpBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
jumpBar.text = "显示";
jumpBar.command = 'novelExt.getJumpingPage'
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
init()
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
let clearOpenDirBook = vscode.commands.registerCommand('novelExt.clearOpenDirBook', () => {
context.globalState.update("novelExt.bookName", undefined);
});
let openDirBook = (/** @type {string} */ msg) => {
let ls = [];
var pattern = /(^\"*)|(\"*$)/g;
console.log(msg.replace(pattern, ""))
let path = msg.replace(pattern, "");
let tmp = fs.readFileSync(path);
let x = tmp.toLocaleString();
x = x.replace(/ +/g, '');
let s = x.split('\r');
s = s.flatMap(it => it.split('\n'));
s = s.filter(it => it.length > 0);
for (let it of s) {
if (it.length < 35) {
ls.push(it);
} else {
let len = it.length;
for (let i = 0; i < len; i += 35) {
ls.push(it.substring(i, i + 35));
}
}
}
console.log("ls", ls);
context.globalState.update("novelExt.text", ls);
console.log("get Book");
context.workspaceState.update('novelExt.currPageNumber', 0);
vscode.window.showInformationMessage("数据已抓取,数据长度" + ls.length);
context.globalState.update("novelExt.bookName", msg);
hideAllBar();
}
let jumpToPage = vscode.commands.registerCommand('novelExt.jumpToPage', () => {
vscode.window.showInputBox({ // 这个对象中所有参数都是可选参数
password: false, // 输入内容是否是密码
ignoreFocusOut: true, // 默认false,设置为true时鼠标点击别的地方输入框不会消失
placeHolder: '章节数,某一段文字', // 在输入框内的提示信息
validateInput: function (text) {
if (text === undefined) { return '章节数,某一段文字'; }
if (text === '') { return '章节数,某一段文字'; }
} // 对输入内容进行验证并返回
}).then(msg => {
console.log("open msg", msg);
// context.workspaceState.update('novelExt.currPageNumber', 0);
/** @type string[] */
let s = context.globalState.get("novelExt.text");
for (let i = 0; i < s.length; i++) {
// console.log(s[i]);
if (s[i].indexOf(msg) > -1) {
vscode.window.showInformationMessage('行数:' + i);
context.workspaceState.update('novelExt.currPageNumber', i);
JumpingPage();
break;
}
}
});
});
let tryOpenDirBook = vscode.commands.registerCommand('novelExt.openDirBook', () => {
let msg = context.globalState.get("novelExt.bookName");
if (msg !== undefined) {
openDirBook(msg);
} else {
vscode.window.showInputBox({ // 这个对象中所有参数都是可选参数
password: false, // 输入内容是否是密码
ignoreFocusOut: true, // 默认false,设置为true时鼠标点击别的地方输入框不会消失
placeHolder: '书籍名称', // 在输入框内的提示信息
validateInput: function (text) {
if (text === undefined) { return '请输入书籍名称'; }
if (text === '') { return '请输入书籍名称'; }
} // 对输入内容进行验证并返回
}).then(msg => {
console.log("open msg", msg);
openDirBook(msg);
});
}
});
// 老板键
let displayCode = vscode.commands.registerCommand('novelExt.displayCode', () => {
console.log('novelExt.displayCode');
vscode.window.setStatusBarMessage('');
hideAllBar();
});
// 下一页
let getNextPage = vscode.commands.registerCommand('novelExt.getNextPage', () => {
console.log('novelExt.getNextPage');
let books = new Book(context);
vscode.window.setStatusBarMessage(books.getNextPage());
showBar();
});
// 上一页
let getPreviousPage = vscode.commands.registerCommand('novelExt.getPreviousPage', () => {
console.log('novelExt.getPreviousPage');
let books = new Book(context);
vscode.window.setStatusBarMessage(books.getPreviousPage());
showBar();
});
let JumpingPage=()=>{
let books = new Book(context);
let tmp = books.getJumpingPage();
console.log(tmp, "tmp");
vscode.window.setStatusBarMessage(tmp);
showBar();
};
// 跳转某个页面
let getJumpingPage = vscode.commands.registerCommand('novelExt.getJumpingPage', () => {
console.log('novelExt.getJumpingPage');
JumpingPage();
});
let showBar=()=>{
prevBar.show()
nextBar.show()
hideBar.show()
jumpBar.hide()
};
let hideAllBar=()=>{
prevBar.hide()
nextBar.hide()
hideBar.hide()
jumpBar.show()
};
context.subscriptions.push(tryOpenDirBook);
context.subscriptions.push(jumpToPage);
context.subscriptions.push(displayCode);
context.subscriptions.push(getPreviousPage);
context.subscriptions.push(getNextPage);
context.subscriptions.push(getJumpingPage);
context.subscriptions.push(clearOpenDirBook);
}
// this method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}
主要功能有打开本地txt,上下页,显示/隐藏,在状态栏中显示按钮,根据关键词跳转到对应行。
发布
可参考官方文档
https://code.visualstudio.com/api/working-with-extensions/publishing-extension#publishing-extensions
标签:插件,记录,vscode,text,novelExt,let,context,page From: https://www.cnblogs.com/CrossAutomaton/p/17132280.html