From 18fb012e4c68bfb9bea9faafa3fbd66fd37b58a1 Mon Sep 17 00:00:00 2001 From: Mike Mhlv Date: Tue, 19 Nov 2024 12:01:52 +0000 Subject: [PATCH 1/4] 5752: error exception --- src/ts/component/block/chat.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ts/component/block/chat.tsx b/src/ts/component/block/chat.tsx index 5cd2ea96eb..abad7d6c3a 100644 --- a/src/ts/component/block/chat.tsx +++ b/src/ts/component/block/chat.tsx @@ -297,6 +297,9 @@ const BlockChat = observer(class BlockChat extends React.Component { + if (!message.records) { + return; + }; message.records.forEach(it => S.Detail.update(rootId, { id: it.id, details: it }, false)); if (callBack) { From c8188222872fe3f802358561d2331905d0a13d7e Mon Sep 17 00:00:00 2001 From: Mike Mhlv Date: Thu, 21 Nov 2024 17:53:40 +0000 Subject: [PATCH 2/4] JS-5752: on paste logic --- src/ts/component/block/chat/form.tsx | 64 ++++++++++++++-------------- src/ts/lib/util/common.ts | 18 ++++++++ 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/ts/component/block/chat/form.tsx b/src/ts/component/block/chat/form.tsx index 006c635522..773e2b8945 100644 --- a/src/ts/component/block/chat/form.tsx +++ b/src/ts/component/block/chat/form.tsx @@ -450,51 +450,48 @@ const ChatForm = observer(class ChatForm extends React.Component { onPaste (e: any) { e.preventDefault(); - const { from } = this.range; + const { from, to } = this.range; const cb = e.clipboardData || e.originalEvent.clipboardData; const text = U.Common.normalizeLineEndings(String(cb.getData('text/plain') || '')); const electron = U.Common.getElectron(); const list = U.Common.getDataTransferFiles((e.clipboardData || e.originalEvent.clipboardData).items).map((it: File) => this.getObjectFromFile(it)).filter(it => { return !electron.isDirectory(it.path); }); + const value = U.Common.stringInsert(this.getTextValue(), text, from, to); - let value = this.getTextValue(); - let url = U.Common.matchUrl(text); - let isLocal = false; - let to = this.range.to; + this.range = { from: to, to: to + text.length }; + this.refEditable.setValue(value); + this.refEditable.setRange(this.range); + this.refEditable.placeholderCheck(); - if (!url) { - url = U.Common.matchLocalPath(text); - isLocal = true; + if (list.length) { + this.addAttachments(list); }; - if (url) { - const param = isLocal ? `file://${url}` : url; - - if (from == to) { - value = U.Common.stringInsert(value, url + ' ', from, from); - to = from + url.length; - }; + this.checkUrls(); + this.onInput(); + }; - this.marks = Mark.adjust(this.marks, from - 1, url.length + 1); - this.marks.push({ type: I.MarkType.Link, range: { from, to }, param}); - this.updateMarkup(value, to + 1, to + 1); - this.addBookmark(param); - } else { - value = U.Common.stringInsert(value, text, from, to); - - to = to + text.length; - this.range = { from: to, to }; - this.refEditable.setValue(value); - this.refEditable.setRange(this.range); - this.refEditable.placeholderCheck(); + checkUrls () { + const text = this.getTextValue(); + const urls = U.Common.getUrlsFromText(text); + if (!urls.length) { + return; }; - if (list.length) { - this.addAttachments(list); - }; + this.removeBookmarks(); - this.onInput(); + window.setTimeout(() => { + for (const url of urls) { + const { from, to, isLocal, value } = url; + const param = isLocal ? `file://${value}` : value; + + this.marks = Mark.adjust(this.marks, from - 1, value.length + 1); + this.marks.push({ type: I.MarkType.Link, range: { from, to }, param}); + this.addBookmark(param, true); + }; + this.updateMarkup(text, this.range.to + 1, this.range.to + 1); + }, 150); }; canDrop (e: any): boolean { @@ -551,7 +548,7 @@ const ChatForm = observer(class ChatForm extends React.Component { this.setState({ attachments: this.checkLimit('attachments', list) }, callBack); }; - addBookmark (url: string) { + addBookmark (url: string, fromText?: boolean) { const add = (param: any) => { const { title, description, url } = param; const item = { @@ -562,6 +559,7 @@ const ChatForm = observer(class ChatForm extends React.Component { source: url, isTmp: true, timestamp: U.Date.now(), + fromText }; this.addAttachments([ item ]); }; @@ -577,7 +575,7 @@ const ChatForm = observer(class ChatForm extends React.Component { removeBookmarks () { const attachments = this.state.attachments || []; - const bookmarks = attachments.filter(it => it.layout == I.ObjectLayout.Bookmark); + const bookmarks = attachments.filter(it => (it.layout == I.ObjectLayout.Bookmark) && it.fromText); let filtered = attachments; bookmarks.forEach(it => { diff --git a/src/ts/lib/util/common.ts b/src/ts/lib/util/common.ts index 3bad68fead..d95945afd4 100644 --- a/src/ts/lib/util/common.ts +++ b/src/ts/lib/util/common.ts @@ -712,6 +712,24 @@ class UtilCommon { return !((y1 + h1 < y2) || (y1 > y2 + h2) || (x1 + w1 < x2) || (x1 > x2 + w2)); }; + getUrlsFromText (text: string): any[] { + const urls = []; + const words = text.split(/[\s\r?\n]+/); + + let offset = 0; + + for (const word of words) { + if (this.matchUrl(word) || this.matchLocalPath(word)) { + const from = text.substring(offset).indexOf(word) + offset; + + offset = from + word.length; + urls.push({ value: word, from, to: offset, isLocal: !!this.matchLocalPath(word) }); + }; + }; + + return urls; + }; + matchUrl (s: string): string { const m = String(s || '').match(/^(?:[a-z]+:(?:\/\/)?)([^\s\/\?#]+)([^\s\?#]+)(?:\?([^#\s]*))?(?:#([^\s]*))?$/gi); return (m && m.length) ? m[0] : ''; From 3f8f161581a798edc1d06bae9ccb0a28331cc382 Mon Sep 17 00:00:00 2001 From: Mike Mhlv Date: Thu, 21 Nov 2024 18:08:35 +0000 Subject: [PATCH 3/4] JS-5752: previews logic --- src/scss/block/chat/attachment.scss | 6 ++++-- src/ts/component/block/chat/attachment/index.tsx | 5 +++-- src/ts/component/block/chat/form.tsx | 6 +++++- src/ts/component/block/chat/message/index.tsx | 1 + 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/scss/block/chat/attachment.scss b/src/scss/block/chat/attachment.scss index 8f453d7b70..37e4e6e642 100644 --- a/src/scss/block/chat/attachment.scss +++ b/src/scss/block/chat/attachment.scss @@ -28,7 +28,7 @@ .descr { display: flex; flex-direction: row; gap: 0px 6px; align-items: center; color: var(--color-text-secondary); } } - .attachment.isBookmark { width: unset !important; min-width: unset; height: unset; } + .attachment.isBookmark { min-width: unset; height: unset; } .attachment.isBookmark { .inner { display: flex; flex-direction: column; padding: 16px; gap: 8px 0px; width: 100%; max-width: 360px; } .inner { @@ -71,7 +71,9 @@ .attachments.isSingle { .attachment { width: 540px; } -} +}.attachments.isSingle.isBookmark { + .attachment { width: 360px; } + } /* Layouts */ diff --git a/src/ts/component/block/chat/attachment/index.tsx b/src/ts/component/block/chat/attachment/index.tsx index fcebd61e3e..341291ea87 100644 --- a/src/ts/component/block/chat/attachment/index.tsx +++ b/src/ts/component/block/chat/attachment/index.tsx @@ -6,6 +6,7 @@ import { I, U, S, J, Action } from 'Lib'; interface Props { object: any; showAsFile?: boolean; + bookmarkAsDefault?: boolean; onRemove: (id: string) => void; onPreview?: (data: any) => void; }; @@ -27,7 +28,7 @@ const ChatAttachment = observer(class ChatAttachment extends React.Component { > {attachments.map(item => ( - + ))} diff --git a/src/ts/component/block/chat/message/index.tsx b/src/ts/component/block/chat/message/index.tsx index 53af4bb044..177e842a92 100644 --- a/src/ts/component/block/chat/message/index.tsx +++ b/src/ts/component/block/chat/message/index.tsx @@ -154,6 +154,7 @@ const ChatMessage = observer(class ChatMessage extends React.Component this.onAttachmentRemove(item.id)} onPreview={(preview) => this.onPreview(preview)} showAsFile={!attachmentsLayout} + bookmarkAsDefault={attachments.length > 1} /> ))} From f068d34d019215bde8a34f4a6adc89e5b0394775 Mon Sep 17 00:00:00 2001 From: Mike Mhlv Date: Thu, 21 Nov 2024 19:48:41 +0000 Subject: [PATCH 4/4] JS-5752: code review --- src/scss/block/chat/attachment.scss | 4 +++- src/ts/component/block/chat.tsx | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/scss/block/chat/attachment.scss b/src/scss/block/chat/attachment.scss index 37e4e6e642..55217a563c 100644 --- a/src/scss/block/chat/attachment.scss +++ b/src/scss/block/chat/attachment.scss @@ -71,7 +71,9 @@ .attachments.isSingle { .attachment { width: 540px; } -}.attachments.isSingle.isBookmark { +} + +.attachments.isSingle.isBookmark { .attachment { width: 360px; } } diff --git a/src/ts/component/block/chat.tsx b/src/ts/component/block/chat.tsx index abad7d6c3a..83a1e3403b 100644 --- a/src/ts/component/block/chat.tsx +++ b/src/ts/component/block/chat.tsx @@ -297,7 +297,7 @@ const BlockChat = observer(class BlockChat extends React.Component { - if (!message.records) { + if (message.error.code) { return; }; message.records.forEach(it => S.Detail.update(rootId, { id: it.id, details: it }, false));