干巴爹兔的博客 干巴爹兔的博客
首页
  • 前端文章

    • JavaScript
    • HTML
    • Vue
  • 学习笔记

    • JavaScript教程
    • React学习笔记
    • Electron学习笔记
  • 开源项目

    • cloud-app-admin
    • 下班了吗Vscode插件
    • Subversion变更单插件
  • Server

    • Django
  • 学习笔记

    • MySQL学习笔记
  • 运维

    • 服务器部署
    • Linux
  • 日常学习

    • 学习方法
关于
收藏
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

干巴爹兔

卑微的前端打工人
首页
  • 前端文章

    • JavaScript
    • HTML
    • Vue
  • 学习笔记

    • JavaScript教程
    • React学习笔记
    • Electron学习笔记
  • 开源项目

    • cloud-app-admin
    • 下班了吗Vscode插件
    • Subversion变更单插件
  • Server

    • Django
  • 学习笔记

    • MySQL学习笔记
  • 运维

    • 服务器部署
    • Linux
  • 日常学习

    • 学习方法
关于
收藏
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • JavaScript文章

  • 学习笔记

  • 开源项目

    • cloud-app-admin
    • could-app-admin前端组件

    • 下班了吗Vscode插件

      • Vscode插件配置项监听
      • Subversion变更单插件

    • HTML

    • Vue

    • 前端
    • 开源项目
    • 下班了吗Vscode插件
    干巴爹兔
    2022-10-18
    目录

    Vscode插件配置项监听

    # 缘由

    自己写了一个督促下班的Vscode插件下班了吗,下班了吗 - Visual Studio Marketplace (opens new window)

    插件可以读取用户的配置,实现例如上下班时间、下班前几分钟提醒等

    之前是使用定时器每秒钟读取一次配置来实现的监听,但是考虑到性能问题,最后在查找文档后发现可以使用vscode.workspace.onDidChangeConfiguration方法来实现配置文件的监听。

    # 核心代码实现

    config.ts

    import * as vscode from 'vscode';
    
    export function getConfiguration<T extends any>(property: string): T {
        return vscode.workspace.getConfiguration('xiabanlema').get(property)!;
    };
    
    
    1
    2
    3
    4
    5
    6

    core/index.ts

    import dayjs = require('dayjs');
    import * as vscode from 'vscode';
    import { getConfiguration } from '../util/config';
    import { calcDuration, formatLocalStringTime } from '../util/time';
    
    // 下班主类
    export default class XiaBan {
        // 底部状态栏
        xiabanStatusItem: vscode.StatusBarItem;
        // 下班指令
        xiabanCommand: string = 'xiabanlema.xiaban';
        // 注册下班指令
        xiabanRegister!: vscode.Disposable;
        // 注册配置变化事件
        xiabanConfiguration!: vscode.Disposable;
    
        constructor() {
    	    this.xiabanStatusItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
    	    this.xiabanStatusItem.command = this.xiabanCommand;
    	    this.xiabanStatusItem.tooltip = '下班了吗?';
            
            this.registerCommand();
        }
    
        // 判断是否快下班
        isOutOfWorkSoon() {
            const notificationTime: number = getConfiguration('notificationTime') || 30;
            const xiabanTimeStr: string = getConfiguration('xiabanTime') || '18:00';
            const xiabanTime = formatLocalStringTime(xiabanTimeStr);
    
            const notificationTimeParse = dayjs(xiabanTime).subtract(notificationTime, 'minute');
            if (dayjs().format('YYYY-MM-DD HH:mm') === notificationTimeParse.format('YYYY-MM-DD HH:mm')) {
                return true;
            }
            return false;
        }
    
        // 事件通知
        notifyFunc() {
            const isOutOfWorkSoon = this.isOutOfWorkSoon();
            const notificationTime: boolean = getConfiguration('notification');
            if (isOutOfWorkSoon && notificationTime) {
                vscode.window.showInformationMessage('下班时间快到啦!');
            }
        }
    
        registerCommand() {
            this.xiabanRegister = vscode.commands.registerCommand(this.xiabanCommand, () => {
                const { isOutOfWork, hours, minutes } = calcDuration();
                if (isOutOfWork) {
                    vscode.window.showInformationMessage('还上啥班啊,下班了!!!');
                } else {
                    const message = `还有${hours}小时${minutes}分钟下班`;
                    vscode.window.showInformationMessage(message);
                }
            });
    
            this.xiabanConfiguration = vscode.workspace.onDidChangeConfiguration(() => {
                this.updateStatusBarItem();
                this.notifyFunc();
            });
            
        }
    
        updateStatusBarItem(): void {
            const { isOutOfWork, hours, minutes } = calcDuration();
            if (isOutOfWork) {
                this.xiabanStatusItem.text = `$(rocket) 下班了`;
            } else {
                const message = `$(symbol-event) ${hours}小时${minutes}分`;
                this.xiabanStatusItem.text = message;
            }
            this.xiabanStatusItem.show();
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75

    extension.ts

    import * as vscode from 'vscode';
    import { setupDayjs } from './util/time';
    import XiaBan from "./core/index";
    import { getConfiguration } from './util/config';
    
    
    let timer: NodeJS.Timer;
    const xiaban = new XiaBan();
    
    export function activate(context: vscode.ExtensionContext) {
    	setupDayjs();
    
    	context.subscriptions.push(xiaban.xiabanStatusItem);
    	context.subscriptions.push(xiaban.xiabanRegister);
    	context.subscriptions.push(xiaban.xiabanConfiguration);
    
    	xiaban.updateStatusBarItem();
    	xiaban.notifyFunc();
    
    	timer && clearInterval(timer);
    	timer = setInterval(() => {
    		xiaban.updateStatusBarItem();
    		// 判断是否需要监听下班时间
    		const notificationTime: boolean = getConfiguration('notification'); 
    		if (notificationTime) {
    			xiaban.notifyFunc();
    		}
    	}, 1000 * 35);
    }
    
    
    export function deactivate() {
    	timer && clearInterval(timer);
    }
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    编辑 (opens new window)
    上次更新: 2023/04/23, 17:02:50
    CTable表格组件
    使用Vscode开发一个小插件

    ← CTable表格组件 使用Vscode开发一个小插件→

    最近更新
    01
    使用Vscode开发一个小插件
    10-21
    02
    使用has属性构造必填效果
    10-14
    03
    CTable表格组件
    10-10
    更多文章>
    Theme by Vdoing | Copyright © 2020-2023 互联网ICP备案: 闽ICP备18027236号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式