create-wirejs-app 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin.js +4 -0
- package/package.json +1 -1
- package/templates/default/src/ssr/simple-wiki/STAR.js +101 -0
package/bin.js
CHANGED
|
@@ -18,6 +18,10 @@ const [
|
|
|
18
18
|
await copy(`${__dirname}/templates/default`, `./${projectName}`);
|
|
19
19
|
await copy(`${__dirname}/templates/default/gitignore`, `./${projectName}/.gitignore`);
|
|
20
20
|
await fs.promises.unlink(`./${projectName}/gitignore`);
|
|
21
|
+
await fs.promises.rename(
|
|
22
|
+
`./${projectName}/src/ssr/simple-wiki/STAR.js`,
|
|
23
|
+
`./${projectName}/src/ssr/simple-wiki/*.js`
|
|
24
|
+
)
|
|
21
25
|
|
|
22
26
|
const packageJson = await fs.readFileSync(`./${projectName}/package.json`);
|
|
23
27
|
fs.writeFileSync(
|
package/package.json
CHANGED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { marked } from 'marked';
|
|
2
|
+
import DOMPurify from 'dompurify';
|
|
3
|
+
import { html, id, text, hydrate, node, list, attribute } from 'wirejs-dom/v2';
|
|
4
|
+
import { accountMenu } from '../../components/account-menu.js';
|
|
5
|
+
import { auth, wiki } from 'my-api';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {{
|
|
9
|
+
* content: string | undefined;
|
|
10
|
+
* user: string | undefined;
|
|
11
|
+
* }}
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
async function Wiki({ context }) {
|
|
15
|
+
const filepath = (context || window).location.pathname;
|
|
16
|
+
const content = await wiki.read(context, filepath);
|
|
17
|
+
|
|
18
|
+
const accountMenuNode = accountMenu(auth);
|
|
19
|
+
let markdown = content ?? `This page doesn't exist yet`;
|
|
20
|
+
const signedOutAction = html`<i>(<b>Sign in</b> to edit.)</i>`;
|
|
21
|
+
const signedInAction = html`<button onclick=${enableEditing}>edit</button>`;
|
|
22
|
+
const invisibleDiv = html`<div style='display: none;'></div>`;
|
|
23
|
+
const editor = html`<div>
|
|
24
|
+
<textarea style='width: 20em; height: 10em;' ${id('textarea')}></textarea>
|
|
25
|
+
</div>`;
|
|
26
|
+
|
|
27
|
+
accountMenuNode.data.onchange(async state => {
|
|
28
|
+
if (state.state.user) {
|
|
29
|
+
self.data.actions = signedInAction;
|
|
30
|
+
} else {
|
|
31
|
+
self.data.actions = signedOutAction;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
function enableEditing() {
|
|
36
|
+
editor.data.textarea.value = markdown;
|
|
37
|
+
self.data.editor = editor;
|
|
38
|
+
self.data.actions = html`<div>
|
|
39
|
+
<button onclick=${submitChanges}>save</button>
|
|
40
|
+
<button onclick=${cancelChanges}>cancel</button>
|
|
41
|
+
</div>`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function submitChanges() {
|
|
45
|
+
markdown = editor.data.textarea.value;
|
|
46
|
+
await wiki.write(context, filepath, markdown);
|
|
47
|
+
self.data.content = markdown;
|
|
48
|
+
self.data.actions = signedInAction;
|
|
49
|
+
self.data.editor = invisibleDiv;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function cancelChanges() {
|
|
53
|
+
self.data.actions = signedInAction;
|
|
54
|
+
self.data.editor = html`<div style='display: none;'></div>`;
|
|
55
|
+
self.data.content = markdown;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const self = html`<div id='wiki'>
|
|
59
|
+
<div style='float: right;'>${accountMenuNode}</div>
|
|
60
|
+
${node('content', markdown, md =>
|
|
61
|
+
html`<div>${DOMPurify.sanitize(marked.parse(md))}</div>`)
|
|
62
|
+
}
|
|
63
|
+
${node('editor', invisibleDiv)}
|
|
64
|
+
${node('actions', signedOutAction)}
|
|
65
|
+
</div>`;
|
|
66
|
+
|
|
67
|
+
return self;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
*
|
|
72
|
+
* @param {import('wirejs-services').Context} context
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
export async function generate(context) {
|
|
76
|
+
const visiblePath = context.location.pathname
|
|
77
|
+
.replaceAll('/', ' > ')
|
|
78
|
+
.replaceAll('<', '<')
|
|
79
|
+
.replaceAll('>', '>')
|
|
80
|
+
.replaceAll('-', ' ')
|
|
81
|
+
.replace(/\s+/g, ' ')
|
|
82
|
+
;
|
|
83
|
+
|
|
84
|
+
const page = html`
|
|
85
|
+
<!doctype html>
|
|
86
|
+
<html>
|
|
87
|
+
<head>
|
|
88
|
+
<title>Wiki ${visiblePath}</title>
|
|
89
|
+
</head>
|
|
90
|
+
<body>
|
|
91
|
+
<p><a href='/'>Home</a></p>
|
|
92
|
+
<h1>Wiki ${visiblePath}</h1>
|
|
93
|
+
${await Wiki({ context })}
|
|
94
|
+
</body>
|
|
95
|
+
</html>
|
|
96
|
+
`;
|
|
97
|
+
|
|
98
|
+
return page;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
hydrate('wiki', Wiki);
|