Skip to content

Commit e53b460

Browse files
agrawal-dchrisbobbe
authored andcommitted
webview: Set up pipeline to render edit history.
Creates `editHistoryHtml.js` and adds styles to `base.css` to generate markup that can be used to show message edit history, in a webview. The CSS for the diff highlight was borrowed from the webapp, but the CSS for rendering the edit history blocks is new.
1 parent fd9c98a commit e53b460

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* @flow strict-local */
2+
import template from './template';
3+
import type { ThemeName, MessageSnapshot, UserOrBot, Auth } from '../../types';
4+
import { shortTime, humanDate } from '../../utils/date';
5+
import getHtml from './html';
6+
7+
type HtmlContent = {
8+
tagHtml: string,
9+
contentHtml: string,
10+
};
11+
12+
const geHtmlFromSnapshot = (snapshot: MessageSnapshot, user: UserOrBot | void): HtmlContent => {
13+
const { prev_content, prev_topic, content_html_diff, topic, rendered_content } = snapshot;
14+
15+
if (prev_content !== undefined && prev_topic === undefined && content_html_diff !== undefined) {
16+
// Only content edited.
17+
return {
18+
tagHtml: template`<span class="message-tag">Edited by ${
19+
user ? user.full_name : 'unknown user'
20+
}</span>`,
21+
contentHtml: content_html_diff,
22+
};
23+
} else if (prev_content === undefined && prev_topic !== undefined) {
24+
// Only topic edited.
25+
return {
26+
tagHtml: template`<span class="message-tag">Topic edited by ${
27+
user ? user.full_name : 'unknown user'
28+
}</span>`,
29+
contentHtml: template`Topic:
30+
<span class="highlight_text_inserted">${topic}</span>
31+
<span class="highlight_text_deleted">${prev_topic}</span>`,
32+
};
33+
} else if (prev_topic !== undefined && content_html_diff !== undefined) {
34+
return {
35+
// Both topic and content edited.
36+
tagHtml: template`<span class="message-tag">Edited by ${
37+
user ? user.full_name : 'unknown user'
38+
}</span>`,
39+
contentHtml: template`Topic:
40+
<span class="highlight_text_inserted">${topic}</span>
41+
<span class="highlight_text_deleted">${prev_topic}</span>
42+
<br/>$!${content_html_diff}`,
43+
};
44+
} else {
45+
// Original message.
46+
return {
47+
tagHtml: template`<span class="message-tag">Original message</span>`,
48+
contentHtml: rendered_content,
49+
};
50+
}
51+
};
52+
53+
const renderSnapshot = (snapshot: MessageSnapshot, usersById: Map<number, UserOrBot>): string => {
54+
const user = usersById.get(snapshot.user_id);
55+
const date = new Date(snapshot.timestamp * 1000);
56+
const editedTime = template`${humanDate(date)} ${shortTime(date)}`;
57+
const { tagHtml, contentHtml } = geHtmlFromSnapshot(snapshot, user);
58+
59+
return template`
60+
<div class="edit-history-block">
61+
<div class="static-timestamp">${editedTime}</div>
62+
<span class="edit-history-content">$!${contentHtml}</span>
63+
<div class="message-tags">$!${tagHtml}</div>
64+
</div>`;
65+
};
66+
67+
export default (
68+
editHistory: MessageSnapshot[],
69+
themeName: ThemeName,
70+
usersById: Map<number, UserOrBot>,
71+
auth: Auth,
72+
): string => {
73+
let html: string = '';
74+
for (const snapshot of editHistory) {
75+
html += renderSnapshot(snapshot, usersById);
76+
}
77+
return getHtml(themeName, { scrollMessageId: null, auth }, html);
78+
};

src/webview/static/base.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,30 @@ hr {
564564
border: 1px dashed hsla(0, 0%, 50%, 0.5);
565565
border-radius: 0.5rem;
566566
}
567+
.edit-history-block{
568+
padding:8px;
569+
border-bottom:1px solid hsla(0, 0%, 50%, 0.25);
570+
}
571+
.edit-history-block .static-timestamp{
572+
margin-top: 8px;
573+
text-align: right;
574+
}
575+
.edit-history-content{
576+
-webkit-user-select: text; /* Safari 3.1+ */
577+
-moz-user-select: text; /* Firefox 2+ */
578+
-ms-user-select: text; /* IE 10+ */
579+
user-select: text; /* Standard syntax */
580+
-khtml-user-select: text;
581+
-webkit-touch-callout: text;
582+
word-wrap: break-word;
583+
}
584+
.highlight_text_inserted {
585+
color: #9effa1;
586+
background-color: hsla(120,64%,95%,.3);
587+
}
588+
.highlight_text_deleted {
589+
color: red;
590+
background-color: #fcdcd8;
591+
text-decoration: line-through;
592+
word-break: break-all;
593+
}

0 commit comments

Comments
 (0)