Skip to content

Commit

Permalink
#879 Fix for auto updating last modified date on save
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Oct 24, 2024
1 parent 0e04e68 commit 6f288ff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### 🐞 Fixes

- [#876](https://github.com/estruyf/vscode-front-matter/issues/876): Fix snippet type on the snippet card
- [#879](https://github.com/estruyf/vscode-front-matter/issues/879): Fix for auto updating last modified date on save

## [10.5.1] - 2024-10-23

Expand Down
17 changes: 13 additions & 4 deletions src/commands/Article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Article {
*
* @param subscriptions - The array of subscriptions to register the commands with.
*/
public static async registerCommands(subscriptions: unknown[]) {
public static registerCommands(subscriptions: unknown[]) {
subscriptions.push(
commands.registerCommand(COMMAND_NAME.setLastModifiedDate, Article.setLastModifiedDate)
);
Expand All @@ -66,6 +66,15 @@ export class Article {
subscriptions.push(commands.registerCommand(COMMAND_NAME.insertSnippet, Article.insertSnippet));
}

/**
* Registers event listeners for the Article class.
*
* @param subscriptions - An array to which the event listener will be added.
*/
public static registerListeners(subscriptions: unknown[]) {
subscriptions.push(workspace.onWillSaveTextDocument(Article.autoUpdate));
}

/**
* Sets the article date
*/
Expand Down Expand Up @@ -369,15 +378,15 @@ export class Article {
* Article auto updater
* @param event
*/
public static async autoUpdate(event: TextDocumentWillSaveEvent) {
public static autoUpdate(event: TextDocumentWillSaveEvent) {
const document = event.document;
if (document && ArticleHelper.isSupportedFile(document)) {
const autoUpdate = Settings.get(SETTING_AUTO_UPDATE_DATE);

// Is article located in one of the content folders
const folders = await Folders.getCachedOrFresh();
const folders = Folders.getCached();
const documentPath = parseWinPath(document.fileName);
const folder = folders.find((f) => documentPath.startsWith(f.path));
const folder = folders?.find((f) => documentPath.startsWith(f.path));
if (!folder) {
return;
}
Expand Down
13 changes: 4 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ContentProvider from './providers/ContentProvider';
import { PagesListener } from './listeners/dashboard';
import { ModeSwitch } from './services/ModeSwitch';
import { PagesParser } from './services/PagesParser';
import { ContentType, Telemetry, Extension } from './helpers';
import { ContentType, Extension } from './helpers';
import * as l10n from '@vscode/l10n';
import {
Backers,
Expand Down Expand Up @@ -121,8 +121,9 @@ export async function activate(context: vscode.ExtensionContext) {
// Register the taxonomy commands
Taxonomy.registerCommands(subscriptions);

// Register all the article commands
// Register all the article commands and listeners
Article.registerCommands(subscriptions);
Article.registerListeners(subscriptions);

// Template creation
Template.registerCommands();
Expand Down Expand Up @@ -181,9 +182,6 @@ export async function activate(context: vscode.ExtensionContext) {
// Automatically run the command
triggerPageUpdate(`main`);

// Listener for file edit changes
subscriptions.push(vscode.workspace.onWillSaveTextDocument(handleAutoDateUpdate));

// Listener for file saves
subscriptions.push(PagesListener.saveFileWatcher());

Expand Down Expand Up @@ -241,16 +239,13 @@ export async function activate(context: vscode.ExtensionContext) {
// Subscribe all commands
subscriptions.push(PanelView, collapseAll, fmStatusBarItem);

// eslint-disable-next-line no-console
console.log(`𝖥𝗋𝗈𝗇𝗍 𝖬𝖺𝗍𝗍𝖾𝗋 𝖢𝖬𝖲 𝖺𝖼𝗍𝗂𝗏𝖺𝗍𝖾𝖽! 𝖱𝖾𝖺𝖽𝗒 𝗍𝗈 𝗌𝗍𝖺𝗋𝗍 𝗐𝗋𝗂𝗍𝗂𝗇𝗀... 👩‍💻🧑‍💻👨‍💻`);
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
export function deactivate() {}

const handleAutoDateUpdate = (e: vscode.TextDocumentWillSaveEvent) => {
Article.autoUpdate(e);
};

const triggerPageUpdate = (location: string) => {
Logger.verbose(`Trigger page update: ${location}`);
pageUpdateDebouncer(() => {
Expand Down

0 comments on commit 6f288ff

Please sign in to comment.