mihailik commited on
Commit
08610a7
·
1 Parent(s): f9686da

chat 5 now?

Browse files
Files changed (2) hide show
  1. chat5.html +2 -0
  2. chat5.js +125 -0
chat5.html ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Loading...
2
+ <script src="chat5.js"></script>
chat5.js ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ // @ts-check
3
+
4
+ function chat5() {
5
+
6
+ function initHTML() {
7
+ const ui = document.createElement('div');
8
+ ui.innerHTML = `
9
+ <div class=chat-log>Loading...</div>
10
+ <div class=chat-input>[Input]</div>
11
+ <style>
12
+ body {
13
+ position: absolute;
14
+ left: 0; top: 0;
15
+ width: 100%; height: 100%;
16
+ margin: 0;
17
+ padding: 0;
18
+ border: none;
19
+ display: grid;
20
+ grid-template: 1fr auto / 1fr;
21
+ }
22
+
23
+ .chat-log .milkdown {
24
+ height: 100%;
25
+ }
26
+ .chat-input .milkdown {
27
+ border-top: 1px solid #4c566a;
28
+ }
29
+ .prose-mirror {
30
+ height: 100%;
31
+ overflow-y: auto;
32
+ }
33
+ </style>
34
+ `;
35
+
36
+ if (!document.body) {
37
+ document.documentElement.appendChild(document.createElement('body'));
38
+ } else {
39
+ cleanBody();
40
+ }
41
+
42
+ for (const elem of [...ui.children]) {
43
+ document.body.appendChild(elem);
44
+ }
45
+ }
46
+
47
+ function cleanBody() {
48
+ for (const elem of [...document.body.childNodes]) {
49
+ if ((elem.tagName || '').toLowerCase() === 'script') continue;
50
+ elem.remove();
51
+ }
52
+ }
53
+
54
+ async function outputMessage(editor, msg) {
55
+ await editor.action(async (ctx) => {
56
+ const { commands } = ctx.get(milkdownCore.sliceKey);
57
+ await commands.insert(msg);
58
+ });
59
+ }
60
+
61
+ function loadScript(src) {
62
+ return new Promise((resolve, reject) => {
63
+ const script = document.createElement('script');
64
+ script.src = src;
65
+ script.onload = resolve;
66
+ script.onerror = reject;
67
+ document.head.appendChild(script);
68
+ });
69
+ }
70
+
71
+ async function runBrowser() {
72
+ alert('runBrowser...');
73
+ window.onerror = (...args) => {
74
+ alert(
75
+ args.map(String).join('\n')
76
+ );
77
+ };
78
+ initHTML();
79
+ document.querySelector('chat-log').innerText = 'Loading Milkdown...';
80
+
81
+ try {
82
+
83
+ await Promise.all([
84
+ loadScript('https://unpkg.com/@milkdown/core/dist/index.umd.js'),
85
+ loadScript('https://unpkg.com/@milkdown/preset-commonmark/dist/index.umd.js'),
86
+ loadScript('https://unpkg.com/@milkdown/theme-nord/dist/index.umd.js')
87
+ ]);
88
+
89
+ const { Editor, EditorStatus } = milkdownCore;
90
+ const { nord } = milkdownThemeNord;
91
+ const { commonmark } = milkdownPresetCommonmark;
92
+
93
+ const editableEditor = await Editor.make()
94
+ .config((ctx) => {
95
+ ctx.set(milkdownCore.rootKey, document.querySelector('.chat-input'));
96
+ ctx.set(milkdownCore.defaultValue, '# Hello, Milkdown!');
97
+ })
98
+ .use(nord)
99
+ .use(commonmark)
100
+ .create();
101
+
102
+ const readonlyEditor = await Editor.make()
103
+ .config((ctx) => {
104
+ ctx.set(milkdownCore.rootKey, document.querySelector('.chat-log'));
105
+ ctx.set(milkdownCore.defaultValue, '');
106
+ ctx.set(milkdownCore.editorViewOptionsKey, { editable: () => false });
107
+ })
108
+ .use(nord)
109
+ .use(commonmark)
110
+ .create();
111
+
112
+ await outputMessage(readonlyEditor, 'Loaded.');
113
+ } catch (error) {
114
+ const errorElem = document.createElement('pre');
115
+ errorElem.innerText = error.stack || String(error);
116
+ document.querySelector('chat-log').appendChild(errorElem);
117
+ }
118
+ }
119
+
120
+ if (typeof window !== 'undefined' && typeof window?.alert === 'function'
121
+ && typeof document !== 'undefined' && typeof document?.createElement === 'function') {
122
+ runBrowser();
123
+ }
124
+ }
125
+ chat5();