From 4f440c9ef5259b0ffb94747e81f5eaf33576eecb Mon Sep 17 00:00:00 2001 From: Asad Date: Mon, 12 Jan 2026 18:19:57 -0500 Subject: [PATCH] feat: override constant defaults; --- CHANGELOG.md | 4 ++++ README.md | 8 +++++++ package.json | 31 +++++++++++++++++++++++++++- src/cacheManagement/lruCache.ts | 10 ++++----- src/cacheManagement/workbookCache.ts | 24 ++++++++++----------- src/excelEditorProvider.ts | 12 +++++++++-- src/webviewContent.ts | 15 +++++++++----- 7 files changed, 79 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17444d5..8010f29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to the "sheetjs-demo" extension will be documented in this file. +## [0.1.1] + - Added control over #rows x #columns to show + - Added control over #worbooks and #worksheets to cache + ## [0.1.0] - Added commands to disable/enable viewer for specific file extensions diff --git a/README.md b/README.md index 852df6e..e117983 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,14 @@ You can easily disable the SheetJS viewer for specific file extensions: **Built-in VSCode**: Right-click any file and select "Open With..." to choose between SheetJS Viewer and other editors. +### Override Constants (Available Settings) +- `sheetjs.maxRows` - Rows displayed per page (default: **1000**) +- `sheetjs.maxColumns` - Columns displayed (default: **100**) +- `sheetjs.maxCachedWorkbooks` - Workbooks kept in memory (default: **10**) +- `sheetjs.maxCachedSheets` - Sheet pages cached (default: **255**) + +Changes take effect when opening a new spreadsheet file (existing open files need to be reopened) + ## Getting Started Want to integrate SheetJS in your own VSCode extension? Check out our [detailed tutorial](https://docs.sheetjs.com/docs/) to learn how to implement these capabilities in your projects. diff --git a/package.json b/package.json index df84984..a202de1 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "Asadbek Karimov ", "publisher": "asadbek", "icon": "img/logo.png", - "version": "0.1.0", + "version": "0.1.1", "license": "Apache-2.0", "bugs": { "url": "https://git.sheetjs.com/asadbek064/sheetjs-vscode-extension/issues" @@ -30,6 +30,35 @@ "activationEvents": [], "main": "./dist/extension.js", "contributes": { + "configuration": { + "title": "SheetJS Viewer", + "properties": { + "sheetjs.maxRows": { + "type": "number", + "default": 1000, + "minimum": 1, + "description": "Maximum number of rows to display per page" + }, + "sheetjs.maxColumns": { + "type": "number", + "default": 100, + "minimum": 1, + "description": "Maximum number of columns to display" + }, + "sheetjs.maxCachedWorkbooks": { + "type": "number", + "default": 10, + "minimum": 1, + "description": "Maximum number of workbooks to keep in cache" + }, + "sheetjs.maxCachedSheets": { + "type": "number", + "default": 255, + "minimum": 1, + "description": "Maximum number of rendered sheets to keep in cache" + } + } + }, "commands": [ { "command": "sheetjs.disableForExtension", diff --git a/src/cacheManagement/lruCache.ts b/src/cacheManagement/lruCache.ts index 23ce39f..839c073 100644 --- a/src/cacheManagement/lruCache.ts +++ b/src/cacheManagement/lruCache.ts @@ -23,7 +23,7 @@ export class LRUCache { if (!this.cache.has(key)) { return undefined; } - + // update access time this.accessTimes.set(key, Date.now()); return this.cache.get(key); @@ -39,7 +39,7 @@ export class LRUCache { if (!this.cache.has(key) && this.cache.size >= this.maxSize) { this.evictLeastRecentlyUsed(); } - + // store the value and update access time this.cache.set(key, value); this.accessTimes.set(key, Date.now()); @@ -105,18 +105,18 @@ export class LRUCache { */ private evictLeastRecentlyUsed(): void { if (this.cache.size === 0) { return; } - + // find the oldest entry let oldestKey: K | undefined; let oldestTime = Infinity; - + for (const [key, time] of this.accessTimes.entries()) { if (time < oldestTime) { oldestTime = time; oldestKey = key; } } - + // remove the oldest entry if (oldestKey !== undefined) { console.log(`Evicting least recently used cache entry: ${String(oldestKey)}`); diff --git a/src/cacheManagement/workbookCache.ts b/src/cacheManagement/workbookCache.ts index b4f2785..3ade501 100644 --- a/src/cacheManagement/workbookCache.ts +++ b/src/cacheManagement/workbookCache.ts @@ -8,7 +8,7 @@ import { LRUCache } from './lruCache'; export class WorkbookCache { private workbookCache: LRUCache; private sheetCache: LRUCache; - + /** * create a new workbook cache * @param maxWorkbooks Maximum number of workbooks to cache @@ -18,7 +18,7 @@ export class WorkbookCache { this.workbookCache = new LRUCache(maxWorkbooks); this.sheetCache = new LRUCache(maxSheets); } - + /** * generate a cache key for a document * @param uri Document URI @@ -27,7 +27,7 @@ export class WorkbookCache { generateKey(uri: vscode.Uri, mtime: number): string { return `${uri.toString()}-${mtime}`; } - + /** * get a workbook from the cache * @param key Cache key @@ -35,7 +35,7 @@ export class WorkbookCache { getWorkbook(key: string): XLSX.WorkBook | undefined { return this.workbookCache.get(key); } - + /** * store a workbook in the cache * @param key Cache key @@ -44,7 +44,7 @@ export class WorkbookCache { setWorkbook(key: string, workbook: XLSX.WorkBook): void { this.workbookCache.set(key, workbook); } - + /** * check if a workbook exists in the cache * @param key Cache key @@ -52,7 +52,7 @@ export class WorkbookCache { hasWorkbook(key: string): boolean { return this.workbookCache.has(key); } - + /** * get sheet HTML from the cache * @param key Sheet cache key @@ -60,7 +60,7 @@ export class WorkbookCache { getSheet(key: string): string | undefined { return this.sheetCache.get(key); } - + /** * store sheet HTML in the cache * @param key Sheet cache key @@ -69,7 +69,7 @@ export class WorkbookCache { setSheet(key: string, html: string): void { this.sheetCache.set(key, html); } - + /** * Check if sheet HTML exists in the cache * @param key Sheet cache key @@ -77,7 +77,7 @@ export class WorkbookCache { hasSheet(key: string): boolean { return this.sheetCache.has(key); } - + /** * generate a sheet cache key * @param baseKey Base workbook key @@ -87,17 +87,17 @@ export class WorkbookCache { generateSheetKey(baseKey: string, sheetName: string, page: number): string { return `${baseKey}-${sheetName}-page-${page}`; } - + /** * clear all caches for a specific URI * @param uriString URI string prefix to clear */ clearCachesForUri(uriString: string): void { console.log(`Clearing caches for ${uriString}`); - + // clear workbook cache entries for this URI this.workbookCache.deleteByPredicate(key => key.startsWith(uriString)); - + // clear sheet cache entries for this URI this.sheetCache.deleteByPredicate(key => key.startsWith(uriString)); } diff --git a/src/excelEditorProvider.ts b/src/excelEditorProvider.ts index 2c81a6d..0eac9bb 100644 --- a/src/excelEditorProvider.ts +++ b/src/excelEditorProvider.ts @@ -19,7 +19,10 @@ export class ExcelEditorProvider implements vscode.CustomReadonlyEditorProvider< } constructor(private readonly context: vscode.ExtensionContext) { - this.cache = new WorkbookCache(20, 255); + const config = vscode.workspace.getConfiguration('sheetjs'); + const maxWorkbooks = config.get('maxCachedWorkbooks', 10); + const maxSheets = config.get('maxCachedSheets', 255); + this.cache = new WorkbookCache(maxWorkbooks, maxSheets); } async openCustomDocument(uri: vscode.Uri): Promise { @@ -213,8 +216,13 @@ export class ExcelEditorProvider implements vscode.CustomReadonlyEditorProvider< `; + // get config values + const config = vscode.workspace.getConfiguration('sheetjs'); + const maxRows = config.get('maxRows', 1000); + const maxColumns = config.get('maxColumns', 100); + // setup up the HTML with JS to handle sheet switching - webviewPanel.webview.html = getExcelViewerHtml(sheetNames, sheetSelector); + webviewPanel.webview.html = getExcelViewerHtml(sheetNames, sheetSelector, maxRows, maxColumns); // handle messages from the webview this.setupMessageHandlers(document, webviewPanel, workbook, cacheKey); diff --git a/src/webviewContent.ts b/src/webviewContent.ts index cda9c9f..bd439fc 100644 --- a/src/webviewContent.ts +++ b/src/webviewContent.ts @@ -181,12 +181,12 @@ const loadingScript = ` }); `; -function getExcelViewerScript(sheetNames: string[]) { +function getExcelViewerScript(sheetNames: string[], maxRows: number, maxColumns: number) { return ` // config const config = { - rowsPerPage: 500, - maxColumns: 100 + rowsPerPage: ${maxRows}, + maxColumns: ${maxColumns} }; // Store sheet names and state @@ -405,7 +405,12 @@ export function getErrorViewHtml(error: any): string { } // HTML for Excel viewer -export function getExcelViewerHtml(sheetNames: string[], sheetSelector: string): string { +export function getExcelViewerHtml( + sheetNames: string[], + sheetSelector: string, + maxRows: number = 1000, + maxColumns: number = 100 +): string { const body = `
@@ -437,5 +442,5 @@ export function getExcelViewerHtml(sheetNames: string[], sheetSelector: string):
`; - return createHtmlDocument(excelViewerStyles, body, getExcelViewerScript(sheetNames)); + return createHtmlDocument(excelViewerStyles, body, getExcelViewerScript(sheetNames, maxRows, maxColumns)); } \ No newline at end of file