同步插件源代码并补齐标准项目文件

This commit is contained in:
XIGE
2026-11-26 15:05:10 +08:00
parent bc412d6a7a
commit 36b20f2dd6
11 changed files with 428 additions and 0 deletions

29
.gitignore vendored Normal file
View File

@@ -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

7
CHANGELOG.md Normal file
View File

@@ -0,0 +1,7 @@
# 变更记录
## 2026-11-26 · 初始同步
- 同步插件源代码到新 Gitea 仓库。
- 补齐 README、LICENSE、.gitignore、CONTRIBUTING 等标准项目文件。
- 本仓库仅包含插件实现代码与必要资源,排除依赖、构建工具、编译产物、环境配置与备份数据。

19
CONTRIBUTING.md Normal file
View File

@@ -0,0 +1,19 @@
# 贡献指南
欢迎对本插件提出改进建议或提交代码。
## 提交前检查
- 仅提交插件实现代码、资源与必要文档,不提交依赖目录、编译产物、运行时数据或备份文件。
- 保持现有目录结构;如新增平台适配代码,请在 `README.md` 的目录结构中说明。
- 涉及对外服务或凭据的配置不要写入代码或文档。
## 提交流程
1. Fork 仓库并创建功能分支。
2. 完成修改后提交 Pull Request,说明改动目的与影响范围。
3. 重要功能变更请同步更新 `CHANGELOG.md`。
## 问题反馈
请通过仓库 Issue 反馈问题,并尽可能提供插件类型、版本与复现步骤。

21
LICENSE Normal file
View File

@@ -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.

View File

@@ -1,2 +1,32 @@
# TimeStamper # 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` 及源码目录放入 `<Vault>/.obsidian/plugins/<插件 id>/`。
3. 在 Obsidian「设置 → 第三方插件」中启用该插件。
## 开发与贡献
详见 [CONTRIBUTING.md](CONTRIBUTING.md)。
## 授权
授权条款见 [LICENSE](LICENSE)(MIT License)。第三方组件版权归各自作者所有。
## 变更记录
详见 [CHANGELOG.md](CHANGELOG.md)。

10
manifest.json Normal file
View File

@@ -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"
}

BIN
res/dialog.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

BIN
res/hotkeys.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

BIN
res/settings.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

279
src/main.ts Normal file
View File

@@ -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();
}));
}
}

33
styles.css Normal file
View File

@@ -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;
}