Skip to content

Commit a6a340e

Browse files
committed
editor input
1 parent 0e424eb commit a6a340e

File tree

7 files changed

+167
-6
lines changed

7 files changed

+167
-6
lines changed

exampleVault/examples fg.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ rating: 31
33
title: test title test test
44
completed: false
55
toggle1: true
6-
slider1: 9
6+
slider1: 71
77
slider2: 8
88
text1: Testa
99
text_area1: test test test
@@ -13,9 +13,20 @@ nested:
1313
object: dfgdf
1414
multi_select:
1515
- option b
16-
- option d
1716
time1: 10:17
1817
suggest: test
18+
editor: |-
19+
test **test**
20+
[[other note]]
21+
22+
## test
23+
24+
> [!INFO]
25+
> this is an info
26+
27+
`INPUT[slider(addLabels):slider1]`
28+
29+
`$= dv.current().rating`
1930
---
2031

2132
## Components
@@ -84,6 +95,9 @@ option(option d)
8495
### Suggester
8596
`INPUT[suggester(suggestOption(test), suggestOptionQuery(#test)):suggest]`
8697

98+
### Editor
99+
`INPUT[editor:editor]`
100+
87101
## In callouts
88102
> quote
89103
> `INPUT[slider(addLabels, minValue(1), maxValue(10)):slider1]`
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<script lang="ts">
2+
import {MarkdownRenderer} from 'obsidian';
3+
import {EditorInputField} from './EditorInputField';
4+
import {onMount} from 'svelte';
5+
6+
export let onValueChange: (value: any) => void;
7+
export let editorInput: EditorInputField;
8+
9+
let value: string;
10+
let focus: boolean = false;
11+
let renderEl: HTMLElement;
12+
let inputEl: HTMLElement;
13+
14+
$: onValueChange(value) && render();
15+
16+
onMount(() => {
17+
renderEl.toggleVisibility(true);
18+
inputEl.toggleVisibility(false);
19+
});
20+
21+
export function updateValue(v: string): void {
22+
value = v;
23+
renderEl.innerHTML = '';
24+
render();
25+
}
26+
27+
function render() {
28+
MarkdownRenderer.renderMarkdown(value, renderEl, editorInput.inputFieldMarkdownRenderChild.filePath, editorInput.inputFieldMarkdownRenderChild);
29+
}
30+
31+
function focusOut(event: MouseEvent) {
32+
renderEl.toggleVisibility(true);
33+
inputEl.toggleVisibility(false);
34+
renderEl.innerHTML = '';
35+
render();
36+
}
37+
38+
function focusIn(event: MouseEvent) {
39+
console.log('focus in');
40+
renderEl.toggleVisibility(false);
41+
inputEl.toggleVisibility(true);
42+
inputEl.focus();
43+
}
44+
45+
function log() {
46+
console.log('focus');
47+
}
48+
</script>
49+
50+
<style>
51+
.editor-input {
52+
width: 100%;
53+
height: 500px;
54+
padding: 0;
55+
position: relative;
56+
}
57+
58+
.editor-input > textarea {
59+
background: var(--background-secondary);
60+
border: none;
61+
padding: var(--size-4-4) var(--size-4-8);
62+
margin: 0;
63+
position: absolute;
64+
inset: 0;
65+
resize: none;
66+
}
67+
68+
.editor-input > div {
69+
padding: var(--size-4-4) var(--size-4-8);
70+
position: absolute;
71+
inset: 0;
72+
}
73+
</style>
74+
75+
<div class="editor-input card" on:click={(event) => focusIn(event)}>
76+
<textarea bind:this={inputEl} bind:value={value} on:focusout={(event) => focusOut(event)}></textarea>
77+
<div bind:this={renderEl}></div>
78+
</div>
79+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { AbstractInputField } from '../AbstractInputField';
2+
import EditorInput from './EditorInput.svelte';
3+
import { InputFieldMarkdownRenderChild } from '../../InputFieldMarkdownRenderChild';
4+
import { MetaBindInternalError } from '../../utils/MetaBindErrors';
5+
6+
export class EditorInputField extends AbstractInputField {
7+
container: HTMLDivElement | undefined;
8+
component: EditorInput | undefined;
9+
value: string;
10+
11+
constructor(inputFieldMarkdownRenderChild: InputFieldMarkdownRenderChild, onValueChange: (value: any) => void | Promise<void>) {
12+
super(inputFieldMarkdownRenderChild, onValueChange);
13+
14+
this.value = '';
15+
}
16+
17+
getValue(): string {
18+
return this.value;
19+
}
20+
21+
setValue(value: any): void {
22+
this.value = value;
23+
this.component?.updateValue(value);
24+
}
25+
26+
isEqualValue(value: any): boolean {
27+
return this.value == value;
28+
}
29+
30+
getDefaultValue(): any {
31+
return '';
32+
}
33+
34+
getHtmlElement(): HTMLElement {
35+
if (!this.container) {
36+
throw new MetaBindInternalError('');
37+
}
38+
39+
return this.container;
40+
}
41+
42+
render(container: HTMLDivElement): void {
43+
console.debug(`meta-bind | EditorInputField >> render ${this.inputFieldMarkdownRenderChild.uuid}`);
44+
45+
this.container = container;
46+
47+
this.value = this.inputFieldMarkdownRenderChild.getInitialValue();
48+
49+
this.component = new EditorInput({
50+
target: container,
51+
props: {
52+
onValueChange: this.onValueChange.bind(this),
53+
editorInput: this,
54+
},
55+
});
56+
57+
this.component.updateValue(this.value);
58+
}
59+
}

src/inputFields/InputFieldFactory.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { DatePickerInputField } from './DatePicker/DatePickerInputField';
1313
import { NumberInputField } from './NumberInputField';
1414
import { SuggestInputField } from './Suggest/SuggestInputField';
1515
import { MetaBindParsingError } from '../utils/MetaBindErrors';
16+
import { EditorInputField } from './Editor/EditorInputField';
1617

1718
export class InputFieldFactory {
1819
static allowCodeBlockMap: Record<string, { codeBlock: boolean; inlineCodeBlock: boolean }> = {
@@ -60,6 +61,10 @@ export class InputFieldFactory {
6061
codeBlock: SuggestInputField.allowCodeBlock,
6162
inlineCodeBlock: SuggestInputField.allowInlineCodeBlock,
6263
},
64+
[InputFieldType.EDITOR]: {
65+
codeBlock: EditorInputField.allowCodeBlock,
66+
inlineCodeBlock: EditorInputField.allowInlineCodeBlock,
67+
},
6368
};
6469

6570
static createInputField(
@@ -99,6 +104,9 @@ export class InputFieldFactory {
99104
} else if (inputFieldType === InputFieldType.SUGGESTER) {
100105
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
101106
return new SuggestInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
107+
} else if (inputFieldType === InputFieldType.EDITOR) {
108+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
109+
return new EditorInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
102110
}
103111

104112
return undefined;

src/inputFields/Suggest/SuggestInputField.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { InputFieldMarkdownRenderChild } from '../../InputFieldMarkdownRenderChild';
22
import { AbstractInputField } from '../AbstractInputField';
3-
import Suggest from './Suggest.svelte';
3+
import SuggestInput from './SuggestInput.svelte';
44
import { InputFieldArgumentType } from '../../parsers/InputFieldDeclarationParser';
55
import { DataArray, getAPI, Literal } from 'obsidian-dataview';
66
import { AbstractInputFieldArgument } from '../../inputFieldArguments/AbstractInputFieldArgument';
@@ -15,7 +15,7 @@ export interface SuggestOption {
1515

1616
export class SuggestInputField extends AbstractInputField {
1717
container: HTMLDivElement | undefined;
18-
component: Suggest | undefined;
18+
component: SuggestInput | undefined;
1919
value: string;
2020
options: SuggestOption[];
2121

@@ -33,7 +33,7 @@ export class SuggestInputField extends AbstractInputField {
3333
}
3434

3535
getValue(): string {
36-
return '';
36+
return this.value;
3737
}
3838

3939
setValue(value: any): void {
@@ -111,7 +111,7 @@ export class SuggestInputField extends AbstractInputField {
111111

112112
this.value = this.inputFieldMarkdownRenderChild.getInitialValue();
113113

114-
this.component = new Suggest({
114+
this.component = new SuggestInput({
115115
target: container,
116116
props: {
117117
showSuggest: () => this.showSuggest(),

src/parsers/InputFieldDeclarationParser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export enum InputFieldType {
1616
DATE_PICKER = 'date_picker',
1717
NUMBER = 'number',
1818
SUGGESTER = 'suggester',
19+
EDITOR = 'editor',
1920

2021
INVALID = 'invalid',
2122
}

0 commit comments

Comments
 (0)