mihailik commited on
Commit
5d4928f
·
1 Parent(s): 8ad2be8

Correct handling of the errors.

Browse files
Files changed (1) hide show
  1. chat5.js +57 -16
chat5.js CHANGED
@@ -77,34 +77,53 @@ body {
77
  if (chatLog) chatLog.textContent = 'Loading Milkdown...';
78
 
79
  try {
80
- const kitCore = await import('https://esm.sh/@milkdown/[email protected]/core');
81
- const kitCommonmark = await import('https://esm.sh/@milkdown/kit@7.15.3/preset/commonmark');
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  if (chatLog) chatLog.innerHTML = '';
84
  if (chatInput) chatInput.innerHTML = '';
85
 
 
 
 
86
  // Create read-only editor in .chat-log
87
- const chatLogEditor = await kitCore.Editor.make()
88
  .config((ctx) => {
89
- ctx.set(kitCore.rootCtx, chatLog);
90
- ctx.set(kitCore.defaultValueCtx, 'Loaded.');
91
- ctx.set(kitCore.editorViewOptionsCtx, { editable: () => false });
92
  })
93
  .use(kitCommonmark.commonmark)
94
  .create();
95
 
96
  // Create editable editor in .chat-input, no placeholder, starts empty
97
- const chatInputEditor = await kitCore.Editor.make()
98
  .config((ctx) => {
99
- ctx.set(kitCore.rootCtx, chatInput);
100
- ctx.set(kitCore.defaultValueCtx, '');
101
  })
102
  .use(kitCommonmark.commonmark)
103
  .create();
104
-
105
  return {
106
  kitCore,
107
  kitCommonmark,
 
 
 
108
  chatLogEditor,
109
  chatInputEditor
110
  };
@@ -117,11 +136,29 @@ body {
117
  }
118
  }
119
 
120
- async function outputMessage(chatLogEditor, msg) {
121
- await chatLogEditor.action(async (ctx) => {
122
- const { commands } = ctx.get(milkdownCore.sliceKey);
123
- await commands.insert(msg);
 
 
 
 
 
 
 
 
 
124
  });
 
 
 
 
 
 
 
 
 
125
  }
126
 
127
  async function runBrowser() {
@@ -129,11 +166,15 @@ body {
129
  alert(args.map(String).join('\n'));
130
  };
131
  const { kitCore, kitCommonmark, chatLog, chatInput } = initHTML();
132
- const { chatLogEditor, chatInputEditor } = await initMilkdown({ chatLog, chatInput });
 
 
 
 
133
 
134
  window.onerror = (...args) => {
135
  try {
136
- outputMessage(chatLogEditor, args.map(String).join('\n'));
137
  } catch (errorNext) {
138
  alert(args.map(String).join('\n') + '\n\n' + errorNext.stack);
139
  }
 
77
  if (chatLog) chatLog.textContent = 'Loading Milkdown...';
78
 
79
  try {
80
+ // Import all necessary Milkdown modules with the same version
81
+ const version = '7.15.3';
82
+ const [
83
+ kitCore,
84
+ kitCommonmark,
85
+ milkdownCore,
86
+ milkdownPresetCommonmark,
87
+ milkdownProse
88
+ ] = await Promise.all([
89
+ import(`https://esm.sh/@milkdown/kit@${version}/core`),
90
+ import(`https://esm.sh/@milkdown/kit@${version}/preset/commonmark`),
91
+ import(`https://esm.sh/@milkdown/core@${version}`),
92
+ import(`https://esm.sh/@milkdown/preset-commonmark@${version}`),
93
+ import(`https://esm.sh/@milkdown/prose@${version}`)
94
+ ]);
95
 
96
  if (chatLog) chatLog.innerHTML = '';
97
  if (chatInput) chatInput.innerHTML = '';
98
 
99
+ // Use context keys from milkdownCore only
100
+ const { rootCtx, defaultValueCtx, editorViewOptionsCtx } = milkdownCore;
101
+
102
  // Create read-only editor in .chat-log
103
+ const chatLogEditor = await milkdownCore.Editor.make()
104
  .config((ctx) => {
105
+ ctx.set(rootCtx, chatLog);
106
+ ctx.set(defaultValueCtx, 'Loaded.');
107
+ ctx.set(editorViewOptionsCtx, { editable: () => false });
108
  })
109
  .use(kitCommonmark.commonmark)
110
  .create();
111
 
112
  // Create editable editor in .chat-input, no placeholder, starts empty
113
+ const chatInputEditor = await milkdownCore.Editor.make()
114
  .config((ctx) => {
115
+ ctx.set(rootCtx, chatInput);
116
+ ctx.set(defaultValueCtx, '');
117
  })
118
  .use(kitCommonmark.commonmark)
119
  .create();
120
+
121
  return {
122
  kitCore,
123
  kitCommonmark,
124
+ milkdownCore,
125
+ milkdownPresetCommonmark,
126
+ milkdownProse,
127
  chatLogEditor,
128
  chatInputEditor
129
  };
 
136
  }
137
  }
138
 
139
+ async function outputMessage(chatLogEditor, milkdownCore, msg) {
140
+ await chatLogEditor.action((ctx) => {
141
+ const view = ctx.get(milkdownCore.editorViewCtx);
142
+ const parser = ctx.get(milkdownCore.parserCtx);
143
+ const serializer = ctx.get(milkdownCore.serializerCtx);
144
+ const state = view.state;
145
+ // Get current markdown, append new message, and parse
146
+ const currentMarkdown = serializer(state.doc);
147
+ const newMarkdown = currentMarkdown ? (currentMarkdown + '\n' + msg) : msg;
148
+ const doc = parser(newMarkdown);
149
+ // Use replaceWith and doc.content to avoid TransformError
150
+ const tr = state.tr.replaceWith(0, state.doc.content.size, doc.content);
151
+ view.dispatch(tr);
152
  });
153
+ // Scroll chat log to bottom (smooth if possible)
154
+ const chatLogElem = document.querySelector('.chat-log');
155
+ if (chatLogElem) {
156
+ if ('scrollTo' in chatLogElem) {
157
+ chatLogElem.scrollTo({ top: chatLogElem.scrollHeight, behavior: 'smooth' });
158
+ } else {
159
+ chatLogElem.scrollTop = chatLogElem.scrollHeight;
160
+ }
161
+ }
162
  }
163
 
164
  async function runBrowser() {
 
166
  alert(args.map(String).join('\n'));
167
  };
168
  const { kitCore, kitCommonmark, chatLog, chatInput } = initHTML();
169
+ const milkdownResult = await initMilkdown({ chatLog, chatInput });
170
+ if (!milkdownResult) return;
171
+ const { chatLogEditor, chatInputEditor, milkdownCore } = milkdownResult;
172
+
173
+ outputMessage(chatLogEditor, milkdownCore, 'Milkdown editor component is loaded correctly. Please try typing...');
174
 
175
  window.onerror = (...args) => {
176
  try {
177
+ outputMessage(chatLogEditor, milkdownCore, args.map(String).join('\n'));
178
  } catch (errorNext) {
179
  alert(args.map(String).join('\n') + '\n\n' + errorNext.stack);
180
  }