Skip to content

Commit

Permalink
feat:get/set date
Browse files Browse the repository at this point in the history
  • Loading branch information
dcf committed Jul 1, 2024
1 parent 969328e commit 85175cf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
51 changes: 43 additions & 8 deletions src/date-picker/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
import { widget as Widget } from '$:/core/modules/widgets/widget.js';
import { IChangedTiddlers } from 'tiddlywiki';
import { IChangedTiddlers, IParseTreeNode, IWidgetInitialiseOptions, Tiddler } from 'tiddlywiki';
import flatpickr from './flatpickr.min.js';

class DatePickerWidget extends Widget {
id: string = "";
currentTiddler: Tiddler | undefined;
content: string = "";
date: Date = new Date();

execute() {
if (this.attributes) {
this.computeAttributes();
}
const currentTiddlerTitle = this.getVariable("currentTiddler");
this.currentTiddler = $tw.wiki.getTiddler(currentTiddlerTitle);
this.id = this.getAttribute("id", this.id);

if (!this.hasAttribute("id")) {
this.content = "未设置id属性";
return undefined;
}

if (this.currentTiddler == undefined) {
return undefined;
}

if (!this.currentTiddler.hasField(this.id)) {
$tw.wiki.addTiddler(new $tw.Tiddler(this.currentTiddler.fields, { [this.id]: new Date(Date.now()).toString() }));
}

this.date = new Date(this.currentTiddler?.getFieldString(this.id) || "");
if (isNaN(this.date.getTime())) {
this.content = "错误的时间格式";
return undefined;
}

this.content = this.date.toString();
}

showContent : string = "date picker";

refresh(_changedTiddlers: IChangedTiddlers) {
return false;
this.refreshSelf();
return true;
}

render(parent: Element, nextSibling: Element) {
this.parentDomNode = parent;
this.computeAttributes();
this.execute();
const containerElement = $tw.utils.domMaker('span', {
text: this.showContent,
text: this.content,
});
flatpickr(containerElement, {
enableTime: true,
dateFormat: 'Y-m-d H:i',
defaultDate: this.date,
onChange: (selectedDates: Date[], dateStr: string, instance: any) => {
console.log(dateStr);
this.showContent = dateStr;
this.refreshSelf();
console.log(dateStr, selectedDates[0].toString());
if (this.currentTiddler) {
$tw.wiki.addTiddler(new $tw.Tiddler(this.currentTiddler.fields, { [this.id]: selectedDates[0].toString() }));
}
}
});
parent.insertBefore(containerElement, nextSibling);
Expand Down
4 changes: 3 additions & 1 deletion src/date-picker/readme.tid
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ type: text/vnd.tiddlywiki

! date-picker

date picker
date picker

<$DatePicker id="demo"/>
1 change: 1 addition & 0 deletions wiki/tiddlers/$__plugins_gltzeba_date-picker.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions wiki/tiddlers/$__plugins_gltzeba_date-picker.json.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
author: gltzeba
demo: Mon Jul 01 2024 18:51:18 GMT+0800 (中国标准时间)
dependents:
description: date picker
list: readme
name: date-picker
plugin-type: plugin
title: $:/plugins/gltzeba/date-picker
type: application/json
version: 0.0.1
5 changes: 3 additions & 2 deletions wiki/tiddlers/Test.tid
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
1: Mon Jul 01 2024 18:53:02 GMT+0800 (中国标准时间)
created: 20240627183945234
modified: 20240627184004198
modified: 20240701105310346
tags:
title: Test
type: text/vnd.tiddlywiki

<$DatePicker />
<$DatePicker id="1"/>
5 changes: 4 additions & 1 deletion wiki/tiddlywiki.info
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"description": "Basic client-server edition",
"plugins": [],
"plugins": [
"tiddlywiki/filesystem",
"tiddlywiki/tiddlyweb"
],
"themes": [
"tiddlywiki/vanilla"
],
Expand Down

0 comments on commit 85175cf

Please sign in to comment.