Skip to content

Commit c607503

Browse files
committed
fix #35 + codemirror experiments
1 parent cc35603 commit c607503

File tree

11 files changed

+156
-79
lines changed

11 files changed

+156
-79
lines changed

exampleVault/Input Fields/Editor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
editor: |--
2+
editor: |-
33
test
44
55
**test**

exampleVault/Input Fields/Image Suggester.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
---
2-
image: Other/Images/img_butterfly.webp
3-
---
4-
---
52
image: Other/Images/img_flower.webp
63
---
74

package-lock.json

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@types/node": "^16.11.6",
2121
"@typescript-eslint/eslint-plugin": "^5.30.0",
2222
"@typescript-eslint/parser": "^5.30.0",
23+
"@codemirror/view": "^6.7.2",
2324
"builtin-modules": "^3.3.0",
2425
"esbuild": "^0.14.47",
2526
"esbuild-plugin-copy-watch": "^0.0.7",

src/frontmatterDisplay/CmPlugin.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Decoration, DecorationSet, EditorView, PluginSpec, PluginValue, ViewPlugin, ViewUpdate } from '@codemirror/view';
2+
import { FrontmatterWidget } from './FrontmatterWidget';
3+
4+
class CmPlugin implements PluginValue {
5+
decorations: DecorationSet;
6+
7+
constructor(view: EditorView) {
8+
this.decorations = this.buildDecorations(view);
9+
console.log(view);
10+
}
11+
12+
public update(update: ViewUpdate): void {}
13+
14+
public destroy(): void {}
15+
16+
buildDecorations(view: EditorView): DecorationSet {
17+
const widgets: Decoration[] = [];
18+
19+
widgets.push(Decoration.widget({ widget: new FrontmatterWidget() }));
20+
21+
return Decoration.set(widgets.map(x => x.range(0)));
22+
}
23+
}
24+
25+
const pluginSpec: PluginSpec<CmPlugin> = {
26+
decorations: (value: CmPlugin) => value.decorations,
27+
};
28+
29+
export const cmPlugin = ViewPlugin.fromClass(CmPlugin, pluginSpec);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts">
2+
export let frontmatter: Record<string, any>;
3+
</script>
4+
5+
<div class="meta-bind-plugin-card">
6+
7+
</div>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { EditorView, WidgetType } from '@codemirror/view';
2+
import FrontmatterDisplay from './FrontmatterDisplay.svelte';
3+
4+
export class FrontmatterWidget extends WidgetType {
5+
toDOM(view: EditorView): HTMLElement {
6+
const el = document.documentElement.createDiv();
7+
8+
new FrontmatterDisplay({
9+
target: el,
10+
props: {
11+
frontmatter: {},
12+
},
13+
});
14+
15+
return el;
16+
}
17+
}

src/inputFields/DatePicker/DatePickerInputSvelteHelpers.ts

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
11
import { moment } from 'obsidian';
2+
import { mod } from '../../utils/Utils';
23

34
const monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
45

6+
export interface Weekday {
7+
index: number;
8+
name: string;
9+
shortName: string;
10+
}
11+
12+
export const weekdays: Weekday[] = [
13+
{
14+
index: 0,
15+
name: 'Sunday',
16+
shortName: 'Su',
17+
},
18+
{
19+
index: 1,
20+
name: 'Monday',
21+
shortName: 'Mo',
22+
},
23+
{
24+
index: 2,
25+
name: 'Tuesday',
26+
shortName: 'Tu',
27+
},
28+
{
29+
index: 3,
30+
name: 'Wednesday',
31+
shortName: 'We',
32+
},
33+
{
34+
index: 4,
35+
name: 'Thursday',
36+
shortName: 'Th',
37+
},
38+
{
39+
index: 5,
40+
name: 'Friday',
41+
shortName: 'Fr',
42+
},
43+
{
44+
index: 6,
45+
name: 'Saturday',
46+
shortName: 'Sa',
47+
},
48+
];
49+
50+
export let firstWeekday: Weekday = weekdays[1];
51+
52+
export function setFirstWeekday(w: Weekday) {
53+
firstWeekday = w;
54+
}
55+
556
export function getMonthName(index: number): string {
657
return monthNames[index];
758
}
859

960
export function getDateRows(monthIndex: number, year: number): number[] {
1061
const days: number = moment(new Date(year, monthIndex)).daysInMonth(); // amount of days in month
11-
let rows: number[] = new Array(42).fill(undefined); // empty 42 long array
62+
let rows: number[] = new Array(42).fill(undefined); // empty 42 long array (28 + 7 + 7)
1263
const startIndex: number = getWeekDay(new Date(year, monthIndex, 1)); // index offset based on weekday of first day in month
1364

1465
for (let i = 0; i < days; i++) {
@@ -23,12 +74,22 @@ export function getDateRows(monthIndex: number, year: number): number[] {
2374

2475
// first day of the week is monday where I live
2576
export function getWeekDay(date: Date): number {
26-
return date.getDay() === 0 ? 6 : date.getDay() - 1;
77+
return mod(date.getDay() - firstWeekday.index, 7);
2778
}
2879

2980
// first day of the week is monday where I live
3081
export function getWeekDays(): string[] {
31-
return ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];
82+
const sortedWeekdays: Weekday[] = weekdays
83+
.map(x => ({
84+
index: mod(x.index - firstWeekday.index, 7),
85+
name: x.name,
86+
shortName: x.shortName,
87+
}))
88+
.sort((a, b) => {
89+
return a.index - b.index;
90+
});
91+
console.log(sortedWeekdays);
92+
return sortedWeekdays.map(x => x.shortName);
3293
}
3394

3495
export function uuid(): () => number {

src/main.ts

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { MetaBindBindTargetError } from './utils/MetaBindErrors';
99
import { API } from './API';
1010
import { ScriptMarkdownRenderChild } from './ScriptMarkdownRenderChild';
1111
import { plugins } from 'pretty-format';
12+
import { Extension } from '@codemirror/state';
13+
import { cmPlugin } from './frontmatterDisplay/CmPlugin';
14+
import { setFirstWeekday } from './inputFields/DatePicker/DatePickerInputSvelteHelpers';
1215

1316
export default class MetaBindPlugin extends Plugin {
1417
// @ts-ignore defined in `onload`
@@ -23,6 +26,9 @@ export default class MetaBindPlugin extends Plugin {
2326
// @ts-ignore defined in `onload`
2427
api: API;
2528

29+
// @ts-ignore defined in `onload`
30+
editorExtensions: Extension[];
31+
2632
async onload(): Promise<void> {
2733
console.log(`meta-bind | Main >> load`);
2834

@@ -32,6 +38,7 @@ export default class MetaBindPlugin extends Plugin {
3238

3339
DateParser.dateFormat = this.settings.preferredDateFormat;
3440
this.api.parser.parseTemplates(this.settings.inputTemplates);
41+
setFirstWeekday(this.settings.firstWeekday);
3542

3643
this.activeMarkdownInputFields = [];
3744
this.metadataManager = new MetadataManager(this);
@@ -63,72 +70,11 @@ export default class MetaBindPlugin extends Plugin {
6370
ctx.addChild(new ScriptMarkdownRenderChild(el, source, ctx, this));
6471
});
6572

73+
// this.registerEditorExtension(cmPlugin);
74+
6675
this.addSettingTab(new MetaBindSettingTab(this.app, this));
6776
}
6877

69-
/**
70-
* Accessible function for building an input field.
71-
*
72-
* @param {string|InputFieldDeclaration} declaration The input field declaration as a string or object.
73-
* @param {string} sourcePath The path of the file the element will be inserted into.
74-
* @param {HTMLElement} container The container element for the input element.
75-
* @param {RenderChildType} renderType Inline or Code Block.
76-
*
77-
* @returns The render child produced.
78-
*/
79-
// buildInputFieldMarkdownRenderChild(
80-
// declaration: string | InputFieldDeclaration,
81-
// sourcePath: string,
82-
// container: HTMLElement,
83-
// renderType: InputFieldMarkdownRenderChildType = InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK
84-
// ): InputFieldMarkdownRenderChild {
85-
// if (typeof declaration === 'string') {
86-
// declaration = InputFieldDeclarationParser.parseString(declaration);
87-
// } else {
88-
// declaration = InputFieldDeclarationParser.parseDeclaration(declaration);
89-
// }
90-
//
91-
// return new InputFieldMarkdownRenderChild(container, renderType, declaration, this, sourcePath, crypto.randomUUID());
92-
// }
93-
94-
/**
95-
* Helper method to build a declaration from some initial data or a string.
96-
*
97-
* @param {string | InputFieldDeclaration | {}} declarationData The base declaration data or a string to parse for it. Can also be an empty object with the other arguments provided to fill it.
98-
* @param {Record<InputFieldArgumentType, string> | {} | undefined} inputFieldArguments (Optional) The input field arguments, indexed by argument name.
99-
* @param {InputFieldType | undefined} inputFieldType (Optional) The input field type if not provided in the base object.
100-
* @param {boolean | undefined} isBound (Optional) If the field should try to be bound to a bindTarget.
101-
* @param {string | undefined} bindTarget (Optional) The bind target of the field.
102-
* @param {string | undefined} templateName (Optional) A template to use.
103-
*
104-
* @returns A constructed InputFieldDeclaration.
105-
*/
106-
// buildDeclaration(
107-
// declarationData: string | InputFieldDeclaration | {},
108-
// inputFieldArguments?: Record<InputFieldArgumentType, string> | {},
109-
// inputFieldType?: InputFieldType,
110-
// isBound?: boolean,
111-
// bindTarget?: string,
112-
// templateName?: string
113-
// ): InputFieldDeclaration {
114-
// if (typeof declarationData === 'string') {
115-
// return InputFieldDeclarationParser.parseString(declarationData);
116-
// } else {
117-
// const declarationBase = declarationData as InputFieldDeclaration;
118-
// declarationBase.inputFieldType = inputFieldType ?? declarationBase.inputFieldType ?? InputFieldType.INVALID;
119-
// declarationBase.isBound = isBound ?? declarationBase.isBound ?? false;
120-
// declarationBase.bindTarget = bindTarget ?? declarationBase.bindTarget ?? undefined;
121-
//
122-
// // if the input field is bound should be determined by `isBound`
123-
// // `isBound` is true, `bindTarget` must be set
124-
// if (declarationBase.isBound && !declarationBase.bindTarget) {
125-
// throw new MetaBindBindTargetError('input field declaration is bound but bind target is undefined');
126-
// }
127-
//
128-
// return InputFieldDeclarationParser.parseDeclaration(declarationBase, inputFieldArguments, templateName);
129-
// }
130-
// }
131-
13278
onunload(): void {
13379
console.log(`meta-bind | Main >> unload`);
13480
for (const activeMarkdownInputField of this.activeMarkdownInputFields) {
@@ -179,6 +125,7 @@ export default class MetaBindPlugin extends Plugin {
179125

180126
DateParser.dateFormat = this.settings.preferredDateFormat;
181127
this.api.parser.parseTemplates(this.settings.inputTemplates);
128+
setFirstWeekday(this.settings.firstWeekday);
182129
await this.saveData(this.settings);
183130
}
184131
}

src/parsers/InputFieldDeclarationParser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ export class InputFieldDeclarationParser {
230230
}
231231

232232
parseArguments(inputFieldType: InputFieldType, inputFieldArguments?: { type: InputFieldArgumentType; value: string }[] | undefined): InputFieldArgumentContainer {
233-
console.log('-> inputFieldArguments', inputFieldArguments);
234233
const argumentContainer: InputFieldArgumentContainer = new InputFieldArgumentContainer();
235234

236235
if (inputFieldArguments) {

0 commit comments

Comments
 (0)