diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40fbae9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# Dependencies and build output +node_modules/ +vendor/ +dist/ +build/ +out/ +main.js +*.apk + +# Build tools and IDE +.gradle/ +.idea/ +*.iml +gradle/ +gradlew +gradlew.bat + +# Environment and secrets +.env +.env.* +keystore.properties +*.jks +credentials*.json + +# Runtime data and backups +data/ +备份/ +*.log +.DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..a90ef58 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# 变更记录 + +## 2026-11-26 · 初始同步 + +- 同步插件源代码到新 Gitea 仓库。 +- 补齐 README、LICENSE、.gitignore、CONTRIBUTING 等标准项目文件。 +- 本仓库仅包含插件实现代码与必要资源,排除依赖、构建工具、编译产物、环境配置与备份数据。 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..6ed8c08 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,19 @@ +# 贡献指南 + +欢迎对本插件提出改进建议或提交代码。 + +## 提交前检查 + +- 仅提交插件实现代码、资源与必要文档,不提交依赖目录、编译产物、运行时数据或备份文件。 +- 保持现有目录结构;如新增平台适配代码,请在 `README.md` 的目录结构中说明。 +- 涉及对外服务或凭据的配置不要写入代码或文档。 + +## 提交流程 + +1. Fork 仓库并创建功能分支。 +2. 完成修改后提交 Pull Request,说明改动目的与影响范围。 +3. 重要功能变更请同步更新 `CHANGELOG.md`。 + +## 问题反馈 + +请通过仓库 Issue 反馈问题,并尽可能提供插件类型、版本与复现步骤。 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8fc60fe --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License Copyright (c) 2021 Martin Eder + +Permission is hereby granted, free +of charge, to any person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice +(including the next paragraph) shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index ca2d190..94b2be7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,32 @@ # TimeStamper +> 仓库:`TimeStamper` · 类型:Obsidian 插件 · 版本:`1.3.0` · 作者:Martin Eder + +在笔记中插入自定义时间或日期戳。 + +本仓库同步自本地插件源码,**仅包含插件实现代码与必要资源**;不包含依赖、构建工具、编译产物、环境配置或备份数据。 + +## 目录结构 + +- `manifest.json` +- `res/` +- `src/` +- `styles.css` + +## 安装与使用 + +1. 本仓库仅包含插件源代码,未包含依赖与构建工具;请先按 Obsidian 插件标准流程构建生成 `main.js`。 +2. 将 `manifest.json`、构建后的 `main.js`、`styles.css` 及源码目录放入 `/.obsidian/plugins/<插件 id>/`。 +3. 在 Obsidian「设置 → 第三方插件」中启用该插件。 + +## 开发与贡献 + +详见 [CONTRIBUTING.md](CONTRIBUTING.md)。 + +## 授权 + +授权条款见 [LICENSE](LICENSE)(MIT License)。第三方组件版权归各自作者所有。 + +## 变更记录 + +详见 [CHANGELOG.md](CHANGELOG.md)。 diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..ede40b0 --- /dev/null +++ b/manifest.json @@ -0,0 +1,10 @@ +{ + "author": "Martin Eder", + "authorUrl": "https://www.gruware.org", + "description": "Insert customized time/date stamp.", + "id": "obsidian-timestamper", + "isDesktopOnly": false, + "minAppVersion": "0.12.17", + "name": "TimeStamper", + "version": "1.3.0" +} diff --git a/res/dialog.png b/res/dialog.png new file mode 100644 index 0000000..c44e634 Binary files /dev/null and b/res/dialog.png differ diff --git a/res/hotkeys.png b/res/hotkeys.png new file mode 100644 index 0000000..f84c57a Binary files /dev/null and b/res/hotkeys.png differ diff --git a/res/settings.png b/res/settings.png new file mode 100644 index 0000000..519282e Binary files /dev/null and b/res/settings.png differ diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..ba02a86 --- /dev/null +++ b/src/main.ts @@ -0,0 +1,279 @@ +/* Changelog: + * + * V1.0.0 - Initial version + * V1.1.0 - Migrated from dateformat to moment.js + * V1.2.0 - Introduced option for linebreak after insert + * V1.3.0 - Enter-key in the text field closes the dialog and inserts time/date stamp + * V1.3.1 - Fixed time display issue by using moment() directly + * V1.3.2 - Added debug logging to diagnose time issues + * V1.4.0 - Added combined datetime stamp command with direct insertion + * + */ + +import { + App, + ButtonComponent, + Editor, + Modal, + Plugin, + TextComponent, + PluginSettingTab, + Setting, + moment, +} from 'obsidian'; + +interface OtsPluginSettings { + timeStampFormat: string; + dateStampFormat: string; + dateTimeStampFormat: string; // 新增:日期+时间格式 + lastFormat: string; + newLine: boolean; +} + +const DEFAULT_SETTINGS: OtsPluginSettings = { + timeStampFormat: 'HH:mm:ss', + dateStampFormat: 'YYYY-MM-DD', + dateTimeStampFormat: 'YYYY-MM-DD HH:mm:ss', // 默认日期+时间格式 + lastFormat: '', + newLine: false +} + +const logThreshold = 9; +const logger = (logString: string, logLevel=0): void => {if (logLevel <= logThreshold) console.log ('TimeStamper: ' + logString)}; +const version = '1.4.0' + +export default class TimeStamperPlugin extends Plugin { + settings: OtsPluginSettings; + + async onload() { + logger('Loading Plugin v' + version, 1); + logger('Loading Settings... ', 5); + await this.loadSettings(); + logger(' Done', 5); + + this.addSettingTab(new TimeStamperSettingTab(this.app, this)); + + // 保留自定义弹窗命令(可选,用户可能偶尔需要自定义格式) + this.addCommand({ + id: 'obsidian-custom-timestamp', + name: 'Insert custom time/date stamp (with dialog)', + editorCallback: (editor) => { + new TimeStamperModal(this.app, editor, this.settings, this).open(); + }, + }); + + // 一键插入时间 + this.addCommand({ + id: 'obsidian-fast-timestamp', + name: 'Insert time stamp', + editorCallback: (editor) => { + const now = new Date(); + const stamp = moment(now).format(this.settings.timeStampFormat); + if (this.settings.newLine) { + editor.replaceSelection(stamp + '\n'); + } + else { + editor.replaceSelection(stamp); + } + logger('Inserted time stamp: ' + stamp, 9); + } + }); + + // 一键插入日期 + this.addCommand({ + id: 'obsidian-fast-datestamp', + name: 'Insert date stamp', + editorCallback: (editor) => { + const now = new Date(); + const stamp = moment(now).format(this.settings.dateStampFormat); + if (this.settings.newLine) { + editor.replaceSelection(stamp + '\n'); + } + else { + editor.replaceSelection(stamp); + } + logger('Inserted date stamp: ' + stamp, 9); + } + }); + + // 新增:一键插入日期+时间 + this.addCommand({ + id: 'obsidian-fast-datetime-stamp', + name: 'Insert date and time stamp', + editorCallback: (editor) => { + const now = new Date(); + const stamp = moment(now).format(this.settings.dateTimeStampFormat); + if (this.settings.newLine) { + editor.replaceSelection(stamp + '\n'); + } + else { + editor.replaceSelection(stamp); + } + logger('Inserted datetime stamp: ' + stamp, 9); + } + }); + } + + onunload() { + logger('Bye!', 9); + } + + async loadSettings() { + logger('Loading Settings...', 5); + this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); + logger(' - timeStampFormat: ' + this.settings.timeStampFormat, 6); + logger(' - dateStampFormat: ' + this.settings.dateStampFormat, 6); + logger(' - dateTimeStampFormat: ' + this.settings.dateTimeStampFormat, 6); + logger(' - lastFormat: ' + this.settings.lastFormat, 6); + logger(' - newLine: ' + this.settings.newLine, 6); + } + + async saveSettings() { + logger('Saving Settings...', 5); + await this.saveData(this.settings); + logger(' Done.'); + } +} + +class TimeStamperModal extends Modal { + constructor(app: App, editor: Editor, settings: OtsPluginSettings, plugin: Plugin) { + super(app); + this.editor = editor; + this.settings = settings; + this.plugin = plugin; + } + + settings: OtsPluginSettings; + editor: Editor; + plugin: Plugin; + + onOpen() { + const { contentEl, editor, modalEl } = this; + const rowClass = 'row'; + const divClass = 'div'; + const _this = this; + const doStamp = (): void => { + const stampFormat = formatComponent.getValue(); + const now = new Date(); + const stamp = moment(now).format(stampFormat); + if (_this.settings.newLine) { + editor.replaceSelection(stamp + '\n'); + } + else { + editor.replaceSelection(stamp); + } + + _this.settings.lastFormat = stampFormat; + _this.plugin.saveData(_this.settings); + _this.close(); + + editor.scrollIntoView({ + from: editor.getCursor(), + to: editor.getCursor(), + }) + }; + + modalEl.addClass('timestamper-modal'); + + const containerEl = document.createElement(divClass); + containerEl.addClass(rowClass); + + const targetEl = document.createElement(divClass); + targetEl.addClass('input-wrapper'); + + const labelEl = document.createElement(divClass); + labelEl.addClass('input-label'); + labelEl.setText('Format string:'); + + const formatComponent = new TextComponent(targetEl); + formatComponent.setPlaceholder('e.g. YYYY-MM-DD HH:mm:ss'); + formatComponent.setValue(this.settings.lastFormat); + + formatComponent.inputEl.addEventListener('keypress', (keypressed) => { + if (keypressed.key === 'Enter') doStamp(); + }); + + const buttonContainerEl = document.createElement(divClass); + buttonContainerEl.addClass(rowClass); + + const submitButtonTarget = document.createElement(divClass); + submitButtonTarget.addClass('button-wrapper'); + + const submitButtonComponent = new ButtonComponent(submitButtonTarget); + submitButtonComponent.setButtonText('Insert Date/Time Stamp'); + submitButtonComponent.setCta(); + submitButtonComponent.onClick(doStamp); + + containerEl.appendChild(labelEl); + containerEl.appendChild(targetEl); + buttonContainerEl.appendChild(submitButtonTarget); + + contentEl.append(containerEl); + contentEl.append(buttonContainerEl); + + submitButtonComponent.buttonEl.focus(); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +class TimeStamperSettingTab extends PluginSettingTab { + plugin: TimeStamperPlugin; + + constructor(app: App, plugin: TimeStamperPlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + const {containerEl} = this; + containerEl.empty(); + + new Setting(containerEl) + .setName('Date Stamp Template') + .setDesc('Template String for inserting a date stamp') + .addText(text => text + .setValue(this.plugin.settings.dateStampFormat) + .onChange(async (value) => { + logger('Settings update - Date Stamp: ' + value, 5); + this.plugin.settings.dateStampFormat = value; + await this.plugin.saveSettings(); + })); + + new Setting(containerEl) + .setName('Time Stamp Template') + .setDesc('Template String for inserting a time stamp') + .addText(text => text + .setValue(this.plugin.settings.timeStampFormat) + .onChange(async (value) => { + logger('Settings update - Time Stamp: ' + value, 5); + this.plugin.settings.timeStampFormat = value; + await this.plugin.saveSettings(); + })); + + new Setting(containerEl) + .setName('Date + Time Stamp Template') + .setDesc('Template String for inserting a combined date and time stamp') + .addText(text => text + .setValue(this.plugin.settings.dateTimeStampFormat) + .onChange(async (value) => { + logger('Settings update - Date+Time Stamp: ' + value, 5); + this.plugin.settings.dateTimeStampFormat = value; + await this.plugin.saveSettings(); + })); + + new Setting(containerEl) + .setName('Insert line break') + .setDesc('Add a line break after the time/date stamp') + .addToggle(toggle => toggle + .setValue(this.plugin.settings.newLine) + .onChange(async (value) => { + logger('Settings update - Insert Line Break: ' + value, 5); + this.plugin.settings.newLine = value; + await this.plugin.saveSettings(); + })); + } +} \ No newline at end of file diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..9173070 --- /dev/null +++ b/styles.css @@ -0,0 +1,33 @@ +.timestamper-modal .modal-content .row { + display: flex; + align-items: center; + width: 100%; +} + + +.timestamper-modal .modal-content .row .input-label { + display: flex; + justify-content: flex-end; + height: 100%; +} + +.timestamper-modal .modal-content .row .input-wrapper { + display: flex; + align-items: center; + height: 100%; + width: 100%; + margin: 0; +} + + +.timestamper-modal .modal-content .row .input-wrapper input { + width: 100%; +} + + +.timestamper-modal .modal-content .button-wrapper { + display: flex; + justify-content: center; + width: 100%; + margin-top: 24px; +}