-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpost-form.js
More file actions
93 lines (82 loc) · 2.98 KB
/
Copy pathpost-form.js
File metadata and controls
93 lines (82 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { parseFilename } from './lib.js';
export default class CreatePost extends HTMLElement {
constructor () {
super();
this.innerHTML = `
<style>
div.formContainer {
display: flex;
flex-direction: column;
gap: 5px;
margin: 1em 0;
}
</style>
<form id="postForm"></form>
<div class="formContainer">
<label for="titleInput">Title</label>
<input id="titleInput" name="title" form="postForm" required/>
<label for="contentInput">Body</label>
<file-upload></file-upload>
<textarea id="contentInput" name="content" rows="15" form="postForm" required></textarea>
<input type="submit" value="Create" form="postForm"/>
</div>
`;
this._form = this.querySelector('#postForm');
}
connectedCallback () {
this.querySelector('file-upload').addEventListener('onFileSelected', (e) => {
this.dispatchEvent(new CustomEvent('onFileUpload', { detail: e.detail }));
});
this._form.addEventListener('submit', this._onSubmitForm.bind(this));
}
reset () {
this._form.reset();
}
_onSubmitForm (e) {
e.preventDefault();
const originalFilename = this.getAttribute('filename') || null;
let filename = this.querySelector('input[name="title"]').value;
if (!filename.match(/\d\d\d\d-\d\d-\d\d-/)) {
let date = new Date().toISOString().slice(0, 10); // YYYY-mm-dd
if (originalFilename) {
date = parseFilename(originalFilename).date;
}
filename = `${date}-${filename}`;
}
if (!filename.match(/\.md$/)) {
filename = `${filename}.md`;
}
const content = this.querySelector('textarea[name="content"]').value || '';
console.log(`CreatePost.connectedCallback.submit ${filename} ${content}`);
const eventDetail = {
originalFilename,
filename,
content
};
this.dispatchEvent(new CustomEvent('onSubmit', { detail: eventDetail }));
}
appendText (text) {
const textArea = this.querySelector('textarea[name=content]');
const selectionStart = textArea.selectionStart;
textArea.value = textArea.value.slice(0, selectionStart) + text + textArea.value.slice(selectionStart);
textArea.selectionStart = selectionStart;
}
static get observedAttributes () { return ['filename', 'content']; }
async attributeChangedCallback (name, oldValue, newValue) {
console.log('PostForm.attributeChangedCallback');
this._render();
}
_render () {
if (this.hasAttribute('filename')) {
this.querySelector('input[type=submit]').value = 'Update';
const filename = this.getAttribute('filename');
this.querySelector('input[name=title]').value = parseFilename(filename).title || '';
} else {
this.querySelector('input[type=submit]').value = 'Create';
}
if (this.hasAttribute('content')) {
this.querySelector('textarea[name=content]').value = this.getAttribute('content') || '';
}
}
}
customElements.define('post-form', CreatePost);