Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: article comments blacklist #87

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/ContextMenu/DropdownMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ export const DropdownMenu = ({
<MenuItem divider />
{selEnabled && (
<React.Fragment>
<MenuItem eventKey="addToBlacklist" onSelect={onMenuSelect}>
{i18n("cmenu_addToBlacklist")}
</MenuItem>
<MenuItem eventKey="removeFromBlacklist" onSelect={onMenuSelect}>
{i18n("cmenu_removeFromBlacklist")}
</MenuItem>
<MenuItem className="DropdownMenu__Item--quickSearch">
{i18n("cmenu_quickSearch")}{" "}
<span style={{ float: "right" }}>&#9658;</span>
Expand Down
7 changes: 5 additions & 2 deletions src/components/ContextMenu/PrefModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ const DEFAULT_PREFS = {
fontSize: 20,
termSize: { cols: 80, rows: 24 },
termSizeMode: "fixed-term-size",
bbsMargin: 0
bbsMargin: 0,

// blacklist
blacklist: []
};

const PREF_STORAGE_KEY = "pttchrome.pref.v1";
Expand All @@ -64,7 +67,7 @@ export const readValuesWithDefault = () => {
}
};

const writeValues = values => {
export const writeValues = values => {
try {
window.localStorage.setItem(
PREF_STORAGE_KEY,
Expand Down
4 changes: 4 additions & 0 deletions src/components/ContextMenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const menuHandlerByEventKey = {
copy: (pttchrome, { selectedText }) => pttchrome.doCopy(selectedText),
copyAnsi: pttchrome => pttchrome.doCopyAnsi(),
paste: pttchrome => pttchrome.doPaste(),
addToBlacklist: (pttchrome, { selectedText }) =>
pttchrome.doAddToBlacklist(selectedText),
removeFromBlacklist: (pttchrome, { selectedText }) =>
pttchrome.doRemoveFromBlacklist(selectedText),
searchGoogle: (pttchrome, { selectedText }) =>
pttchrome.doSearchGoogle(selectedText),
openUrlNewTab: (pttchrome, { aElement }) =>
Expand Down
6 changes: 6 additions & 0 deletions src/js/en_US_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export const en_US = {
"cmenu_selectAll": {
"message": "Select all"
},
"cmenu_addToBlacklist": {
"message": "Add ID to blacklist"
},
"cmenu_removeFromBlacklist": {
"message": "Remove ID from blacklist"
},
"cmenu_searchGoogle": {
"message": "Search Google for"
},
Expand Down
15 changes: 15 additions & 0 deletions src/js/pttchrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { setTimer } from './util';
import PasteShortcutAlert from '../components/PasteShortcutAlert';
import ConnectionAlert from '../components/ConnectionAlert';
import ContextMenu from '../components/ContextMenu';
import { readValuesWithDefault, writeValues } from "../components/ContextMenu/PrefModal";

function noop() {}

Expand Down Expand Up @@ -352,6 +353,20 @@ App.prototype.switchToEasyReadingMode = function(doSwitch) {
this.view.conn.send(unescapeStr('^L'));
};

App.prototype.doAddToBlacklist = function(str) {
const values = readValuesWithDefault()
values.blacklist = [...new Set([...values.blacklist, str.trim()])];
writeValues(values);
console.log('add to blacklist', str);
};

App.prototype.doRemoveFromBlacklist = function(str) {
const values = readValuesWithDefault()
values.blacklist = values.blacklist.filter((user) => user !== str.trim());
writeValues(values);
console.log('remove from blacklist', str);
};

App.prototype.doCopy = function(str) {
if (str.indexOf('\x1b') < 0) {
str = str.replace(/\r\n/g, '\r');
Expand Down
38 changes: 38 additions & 0 deletions src/js/term_buf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Event } from './event';
import { ColorState } from './term_ui';
import { u2b, b2u, parseStatusRow, parseListRow } from './string_util';
import { readValuesWithDefault } from '../components/ContextMenu/PrefModal';

const termColors = [
// dark
Expand Down Expand Up @@ -360,6 +361,41 @@ TermBuf.prototype = {
this.queueUpdate();
},


filterBlacklist: function() {
const { blacklist } = readValuesWithDefault();

var cols = this.cols;
var rows = this.rows;
var lines = this.lines;
for (var row = 0; row < rows; ++row) {
var line = lines[row];
// search comment blacklist
if (line[2].ch === ' ') {
const idText = line
.filter((ch) => ch.bg === 0 && ch.fg === 3 && ch.bright)
.map((ch) => ch.ch).join('').trim();

if (!idText) continue;
if (blacklist.includes(idText)) {
for (var col = 0; col < cols; ++col) {
const ch = line[col];
// set id to red
if (ch.bg === 0 && ch.fg === 3 && ch.bright) {
ch.fg = 1;
ch.needUpdate = true;
}
// set text to ' '
if (ch.bg === 0 && ch.fg === 3 && !ch.bright) {
ch.ch = ' ';
ch.needUpdate = true;
}
}
}
}
}
},

updateCharAttr: function() {
var cols = this.cols;
var rows = this.rows;
Expand Down Expand Up @@ -787,6 +823,8 @@ TermBuf.prototype = {
clearTimeout(this.timerUpdate);
this.timerUpdate = null;

this.filterBlacklist();

if (this.changed) { // content changed
this.updateCharAttr();

Expand Down
6 changes: 6 additions & 0 deletions src/js/zh_TW_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"cmenu_paste": {
"message": "貼上"
},
"cmenu_addToBlacklist": {
"message": "ID 加到黑名單"
},
"cmenu_removeFromBlacklist": {
"message": "從黑名單移除 ID"
},
"cmenu_selectAll": {
"message": "全選"
},
Expand Down