@lvce-editor/chat-view 1.3.0 → 1.4.0

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.
@@ -1271,10 +1271,91 @@ const getKeyBindings = () => {
1271
1271
  return [];
1272
1272
  };
1273
1273
 
1274
+ const submitRename = state => {
1275
+ const {
1276
+ composerValue,
1277
+ renamingSessionId,
1278
+ sessions
1279
+ } = state;
1280
+ const title = composerValue.trim();
1281
+ if (!renamingSessionId || !title) {
1282
+ return {
1283
+ ...state,
1284
+ renamingSessionId: ''
1285
+ };
1286
+ }
1287
+ const updatedSessions = sessions.map(session => {
1288
+ if (session.id !== renamingSessionId) {
1289
+ return session;
1290
+ }
1291
+ return {
1292
+ ...session,
1293
+ title
1294
+ };
1295
+ });
1296
+ return {
1297
+ ...state,
1298
+ composerValue: '',
1299
+ ignoreNextInput: true,
1300
+ renamingSessionId: '',
1301
+ sessions: updatedSessions
1302
+ };
1303
+ };
1304
+ const submitMessage = state => {
1305
+ const {
1306
+ composerValue,
1307
+ nextMessageId,
1308
+ selectedSessionId,
1309
+ sessions
1310
+ } = state;
1311
+ const text = composerValue.trim();
1312
+ if (!text) {
1313
+ return {
1314
+ ...state,
1315
+ ignoreNextInput: true
1316
+ };
1317
+ }
1318
+ const updatedSessions = sessions.map(session => {
1319
+ if (session.id !== selectedSessionId) {
1320
+ return session;
1321
+ }
1322
+ const message = {
1323
+ id: `message-${nextMessageId}`,
1324
+ role: 'user',
1325
+ text
1326
+ };
1327
+ return {
1328
+ ...session,
1329
+ messages: [...session.messages, message]
1330
+ };
1331
+ });
1332
+ return {
1333
+ ...state,
1334
+ composerValue: '',
1335
+ ignoreNextInput: true,
1336
+ lastSubmittedSessionId: selectedSessionId,
1337
+ nextMessageId: nextMessageId + 1,
1338
+ sessions: updatedSessions
1339
+ };
1340
+ };
1341
+ const handleKeyDown = async (state, key, shiftKey) => {
1342
+ if (key !== 'Enter' || shiftKey) {
1343
+ return state;
1344
+ }
1345
+ if (state.renamingSessionId) {
1346
+ return submitRename(state);
1347
+ }
1348
+ return submitMessage(state);
1349
+ };
1350
+
1274
1351
  const CREATE_SESSION = 'create-session';
1275
1352
  const SESSION_PREFIX = 'session:';
1276
1353
  const RENAME_PREFIX = 'session-rename:';
1277
1354
  const DELETE_PREFIX = 'session-delete:';
1355
+ const SEND = 'send';
1356
+ const handleClickSend = async state => {
1357
+ return handleKeyDown(state, 'Enter', false);
1358
+ };
1278
1359
  const getNextSelectedSessionId = (sessions, deletedId) => {
1279
1360
  if (sessions.length === 0) {
1280
1361
  return '';
@@ -1370,6 +1451,9 @@ const handleClick = async (state, name) => {
1370
1451
  const id = name.slice(DELETE_PREFIX.length);
1371
1452
  return deleteSession(state, id);
1372
1453
  }
1454
+ if (name === SEND) {
1455
+ return handleClickSend(state);
1456
+ }
1373
1457
  return state;
1374
1458
  };
1375
1459
 
@@ -1378,6 +1462,8 @@ const handleClickClose = async () => {
1378
1462
  await invoke('Chat.terminate');
1379
1463
  };
1380
1464
 
1465
+ const handleClickSettings = async () => {};
1466
+
1381
1467
  const handleInput = async (state, value) => {
1382
1468
  if (state.ignoreNextInput) {
1383
1469
  return {
@@ -1391,83 +1477,6 @@ const handleInput = async (state, value) => {
1391
1477
  };
1392
1478
  };
1393
1479
 
1394
- const submitRename = state => {
1395
- const {
1396
- composerValue,
1397
- renamingSessionId,
1398
- sessions
1399
- } = state;
1400
- const title = composerValue.trim();
1401
- if (!renamingSessionId || !title) {
1402
- return {
1403
- ...state,
1404
- renamingSessionId: ''
1405
- };
1406
- }
1407
- const updatedSessions = sessions.map(session => {
1408
- if (session.id !== renamingSessionId) {
1409
- return session;
1410
- }
1411
- return {
1412
- ...session,
1413
- title
1414
- };
1415
- });
1416
- return {
1417
- ...state,
1418
- composerValue: '',
1419
- ignoreNextInput: true,
1420
- renamingSessionId: '',
1421
- sessions: updatedSessions
1422
- };
1423
- };
1424
- const submitMessage = state => {
1425
- const {
1426
- composerValue,
1427
- nextMessageId,
1428
- selectedSessionId,
1429
- sessions
1430
- } = state;
1431
- const text = composerValue.trim();
1432
- if (!text) {
1433
- return {
1434
- ...state,
1435
- ignoreNextInput: true
1436
- };
1437
- }
1438
- const updatedSessions = sessions.map(session => {
1439
- if (session.id !== selectedSessionId) {
1440
- return session;
1441
- }
1442
- const message = {
1443
- id: `message-${nextMessageId}`,
1444
- role: 'user',
1445
- text
1446
- };
1447
- return {
1448
- ...session,
1449
- messages: [...session.messages, message]
1450
- };
1451
- });
1452
- return {
1453
- ...state,
1454
- composerValue: '',
1455
- ignoreNextInput: true,
1456
- lastSubmittedSessionId: selectedSessionId,
1457
- nextMessageId: nextMessageId + 1,
1458
- sessions: updatedSessions
1459
- };
1460
- };
1461
- const handleKeyDown = async (state, key, shiftKey) => {
1462
- if (key !== 'Enter' || shiftKey) {
1463
- return state;
1464
- }
1465
- if (state.renamingSessionId) {
1466
- return submitRename(state);
1467
- }
1468
- return submitMessage(state);
1469
- };
1470
-
1471
1480
  const id = 7201;
1472
1481
  const sendMessagePortToExtensionHostWorker = async port => {
1473
1482
  await sendMessagePortToExtensionHostWorker$1(port, id);
@@ -1513,41 +1522,9 @@ const loadContent = async state => {
1513
1522
  };
1514
1523
  };
1515
1524
 
1516
- const css = `.Chat {
1517
- display: grid;
1518
- grid-template-columns: 220px 1fr;
1519
- grid-template-rows: auto 1fr auto;
1520
- height: 100%;
1521
- }
1522
-
1523
- .ChatHeader {
1524
- align-items: center;
1525
- display: flex;
1526
- gap: 8px;
1527
- justify-content: space-between;
1528
- padding: 8px;
1529
- }
1530
-
1531
- .ChatList {
1532
- border-right: 1px solid var(--Widget-border);
1533
- overflow: auto;
1534
- }
1535
-
1536
- .ChatDetails {
1537
- display: flex;
1538
- flex-direction: column;
1539
- min-height: 0;
1540
- }
1541
-
1542
- .ChatDetailsContent {
1543
- flex: 1;
1544
- overflow: auto;
1545
- padding: 8px;
1546
- }
1547
-
1548
- .MultilineInputBox {
1549
- width: 100%;
1550
- }
1525
+ // TODO render things like scrollbar height,scrollbar offset, textarea height,
1526
+ // list height
1527
+ const css = `
1551
1528
  `;
1552
1529
  const renderCss = (oldState, newState) => {
1553
1530
  return [SetCss, newState.uid, css];
@@ -1866,58 +1843,8 @@ const ChatWelcomeMessage = 'ChatWelcomeMessage';
1866
1843
  const HandleInput = 4;
1867
1844
  const HandleClick = 11;
1868
1845
  const HandleKeyDown = 12;
1869
-
1870
- const getChatDetailsDom = (messagesNodes, composerValue) => {
1871
- return [{
1872
- childCount: 2,
1873
- className: ChatDetails,
1874
- type: Div
1875
- }, {
1876
- childCount: Math.max(messagesNodes.length, 0),
1877
- className: ChatDetailsContent,
1878
- type: Div
1879
- }, ...messagesNodes, {
1880
- childCount: 1,
1881
- className: ChatActions,
1882
- type: Div
1883
- }, {
1884
- childCount: 0,
1885
- className: MultilineInputBox,
1886
- name: 'composer',
1887
- placeholder: 'Type your message. Enter to send, Shift+Enter for newline.',
1888
- rows: 4,
1889
- type: TextArea,
1890
- value: composerValue
1891
- }];
1892
- };
1893
-
1894
- const getChatHeaderDom = sessionsLength => {
1895
- return [{
1896
- childCount: 2,
1897
- className: ChatHeader,
1898
- type: Div
1899
- }, {
1900
- childCount: 2,
1901
- className: ChatActions,
1902
- type: Div
1903
- }, {
1904
- childCount: 1,
1905
- className: Label,
1906
- type: Span
1907
- }, text('Chats'), {
1908
- childCount: 1,
1909
- className: IconButton,
1910
- name: 'create-session',
1911
- role: Button$2,
1912
- tabIndex: 0,
1913
- title: 'New Chat',
1914
- type: Button$1
1915
- }, text('+'), {
1916
- childCount: sessionsLength,
1917
- className: ChatList,
1918
- type: Div
1919
- }];
1920
- };
1846
+ const HandleClickClose = 13;
1847
+ const HandleClickSettings = 14;
1921
1848
 
1922
1849
  const getMessagesDom = messages => {
1923
1850
  if (messages.length === 0) {
@@ -1983,10 +1910,8 @@ const getSessionDom = (session, _selectedSessionId) => {
1983
1910
  const getChatVirtualDom = (sessions, selectedSessionId, composerValue) => {
1984
1911
  const selectedSession = sessions.find(session => session.id === selectedSessionId);
1985
1912
  const messages = selectedSession ? selectedSession.messages : [];
1986
- const chatHeaderDom = getChatHeaderDom(sessions.length);
1987
1913
  const sessionNodes = sessions.flatMap(session => getSessionDom(session));
1988
1914
  const messagesNodes = getMessagesDom(messages);
1989
- const chatDetailsDom = getChatDetailsDom(messagesNodes, composerValue);
1990
1915
  const dom = [{
1991
1916
  childCount: 3,
1992
1917
  className: Viewlet + ' Chat',
@@ -1994,7 +1919,75 @@ const getChatVirtualDom = (sessions, selectedSessionId, composerValue) => {
1994
1919
  onInput: HandleInput,
1995
1920
  onKeyDown: HandleKeyDown,
1996
1921
  type: Div
1997
- }, ...chatHeaderDom, ...sessionNodes, ...chatDetailsDom];
1922
+ }, {
1923
+ childCount: 2,
1924
+ className: ChatHeader,
1925
+ type: Div
1926
+ }, {
1927
+ childCount: 4,
1928
+ className: ChatActions,
1929
+ type: Div
1930
+ }, {
1931
+ childCount: 1,
1932
+ className: Label,
1933
+ type: Span
1934
+ }, text('Chats'), {
1935
+ childCount: 1,
1936
+ className: IconButton,
1937
+ name: 'create-session',
1938
+ role: Button$2,
1939
+ tabIndex: 0,
1940
+ title: 'New Chat',
1941
+ type: Button$1
1942
+ }, text('+'), {
1943
+ childCount: 1,
1944
+ className: IconButton,
1945
+ onClick: HandleClickSettings,
1946
+ role: Button$2,
1947
+ tabIndex: 0,
1948
+ title: 'Settings',
1949
+ type: Button$1
1950
+ }, text('⚙'), {
1951
+ childCount: 1,
1952
+ className: IconButton,
1953
+ onClick: HandleClickClose,
1954
+ role: Button$2,
1955
+ tabIndex: 0,
1956
+ title: 'Close Chat',
1957
+ type: Button$1
1958
+ }, text('×'), {
1959
+ childCount: sessions.length,
1960
+ className: ChatList,
1961
+ type: Div
1962
+ }, ...sessionNodes, {
1963
+ childCount: 2,
1964
+ className: ChatDetails,
1965
+ type: Div
1966
+ }, {
1967
+ childCount: Math.max(messagesNodes.length, 0),
1968
+ className: ChatDetailsContent,
1969
+ type: Div
1970
+ }, ...messagesNodes, {
1971
+ childCount: 2,
1972
+ className: ChatActions,
1973
+ type: Div
1974
+ }, {
1975
+ childCount: 0,
1976
+ className: MultilineInputBox,
1977
+ name: 'composer',
1978
+ placeholder: 'Type your message. Enter to send, Shift+Enter for newline.',
1979
+ rows: 4,
1980
+ type: TextArea,
1981
+ value: composerValue
1982
+ }, {
1983
+ childCount: 1,
1984
+ className: Button,
1985
+ name: 'send',
1986
+ role: Button$2,
1987
+ tabIndex: 0,
1988
+ title: 'Send message',
1989
+ type: Button$1
1990
+ }, text('Send')];
1998
1991
  return dom;
1999
1992
  };
2000
1993
 
@@ -2059,6 +2052,12 @@ const renderEventListeners = () => {
2059
2052
  return [{
2060
2053
  name: HandleClick,
2061
2054
  params: ['handleClick', TargetName]
2055
+ }, {
2056
+ name: HandleClickClose,
2057
+ params: ['handleClickClose']
2058
+ }, {
2059
+ name: HandleClickSettings,
2060
+ params: ['handleClickSettings']
2062
2061
  }, {
2063
2062
  name: HandleInput,
2064
2063
  params: ['handleInput', TargetValue]
@@ -2094,6 +2093,28 @@ const saveState = state => {
2094
2093
  };
2095
2094
  };
2096
2095
 
2096
+ const dummySessions = [{
2097
+ id: 'session-a',
2098
+ messages: [],
2099
+ title: 'Dummy Chat A'
2100
+ }, {
2101
+ id: 'session-b',
2102
+ messages: [],
2103
+ title: 'Dummy Chat B'
2104
+ }, {
2105
+ id: 'session-c',
2106
+ messages: [],
2107
+ title: 'Dummy Chat C'
2108
+ }];
2109
+ const setChatList = state => {
2110
+ return {
2111
+ ...state,
2112
+ nextSessionId: dummySessions.length + 1,
2113
+ selectedSessionId: dummySessions[0].id,
2114
+ sessions: dummySessions
2115
+ };
2116
+ };
2117
+
2097
2118
  const commandMap = {
2098
2119
  'Chat.create': create,
2099
2120
  'Chat.diff2': diff2,
@@ -2101,6 +2122,7 @@ const commandMap = {
2101
2122
  'Chat.getKeyBindings': getKeyBindings,
2102
2123
  'Chat.handleClick': wrapCommand(handleClick),
2103
2124
  'Chat.handleClickClose': handleClickClose,
2125
+ 'Chat.handleClickSettings': handleClickSettings,
2104
2126
  'Chat.handleInput': wrapCommand(handleInput),
2105
2127
  'Chat.handleKeyDown': wrapCommand(handleKeyDown),
2106
2128
  'Chat.initialize': initialize,
@@ -2110,6 +2132,7 @@ const commandMap = {
2110
2132
  'Chat.renderEventListeners': renderEventListeners,
2111
2133
  'Chat.resize': wrapCommand(resize),
2112
2134
  'Chat.saveState': wrapGetter(saveState),
2135
+ 'Chat.setChatList': wrapCommand(setChatList),
2113
2136
  'Chat.terminate': terminate
2114
2137
  };
2115
2138
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-view",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Chat View Worker",
5
5
  "repository": {
6
6
  "type": "git",