Skip to content

Commit

Permalink
Add ability to change the delimiter of copied items #40
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Nov 8, 2023
1 parent 6ccf805 commit 9240be8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
5 changes: 5 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
"config_modeForNoSelection_treeDescendants_label": { "message": "Copy Tree Descendants" },
"config_modeForNoSelectionModified_label": { "message": "Middle Click" },

"config_delimiter_caption": { "message": "Separator of copied items" },
"config_delimiter_lineBreak_label": { "message": "Line Break" },
"config_delimiter_space_label": { "message": "Space" },
"config_delimiter_tab_label": { "message": "Tab" },

"config_debug_caption": { "message": "Development" },
"config_debug_label": { "message": "Debug mode" },
"config_all_caption": { "message": "All Configs" }
Expand Down
5 changes: 5 additions & 0 deletions _locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
"config_modeForNoSelection_treeDescendants_label": { "message": "配下のタブのみをコピー" },
"config_modeForNoSelectionModified_label": { "message": "中ボタンクリック" },

"config_delimiter_caption": { "message": "コピーした項目の区切り文字" },
"config_delimiter_lineBreak_label": { "message": "改行" },
"config_delimiter_space_label": { "message": "スペース" },
"config_delimiter_tab_label": { "message": "タブ" },

"config_debug_caption": { "message": "開発用" },
"config_debug_label": { "message": "デバッグモード" },
"config_all_caption": { "message": "すべての設定" }
Expand Down
39 changes: 29 additions & 10 deletions common/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,29 @@ import {
notify,
collectAncestors,
} from './common.js';
import * as Constants from './constants.js';
import * as Permissions from './permissions.js';

const kFORMAT_PARAMETER_MATCHER = /\([^\)]+\)|\[[^\]]+\]|\{[^\}]+\}|<[^>]+>/g;
const kFORMAT_MATCHER_TREE_INDENT = new RegExp(`%(TST|TREE)_INDENT(?:${kFORMAT_PARAMETER_MATCHER.source})*%`, 'gi');

function getDelimiter() {
switch (configs.delimiter) {
case Constants.kDELIMITER_SPACE:
return ' ';

case Constants.kDELIMITER_TAB:
return '\t';

default:
return getLineFeed();
}
}

function getLineFeed() {
return configs.useCRLF ? '\r\n' : '\n';
}

export async function copyToClipboard(tabs, format) {
let indentLevels = [];
if (kFORMAT_MATCHER_TREE_INDENT.test(format)) {
Expand All @@ -34,13 +52,14 @@ export async function copyToClipboard(tabs, format) {
}
}

const lineFeed = configs.useCRLF ? '\r\n' : '\n' ;
const delimiter = getDelimiter();
const itemsToCopy = await Promise.all(tabs.map((tab, index) => fillPlaceHolders(format, tab, indentLevels[index])));

const richText = /%RT%/i.test(format) ? itemsToCopy.map(item => item.richText).join('<br />') : null ;
let plainText = itemsToCopy.map(item => item.plainText).join(lineFeed);
if (tabs.length > 1)
plainText += lineFeed;
let plainText = itemsToCopy.map(item => item.plainText).join(delimiter);
if (configs.delimiter == Constants.kDELIMITER_LINE_BREAK &&
tabs.length > 1)
plainText += delimiter;

log('richText: ', richText);
log('plainText: ', plainText);
Expand Down Expand Up @@ -201,7 +220,7 @@ export async function fillPlaceHolders(format, tab, indentLevel) {
let params = {
tab,
indentLevel,
lineFeed: configs.useCRLF ? '\r\n' : '\n',
delimiter: getDelimiter(),
timeUTC: now.toUTCString(),
timeLocal: now.toLocaleString()
};
Expand Down Expand Up @@ -274,14 +293,14 @@ export async function fillPlaceHolders(format, tab, indentLevel) {

function fillPlaceHoldersInternal(
format,
{ tab, author, description, keywords, timeUTC, timeLocal, lineFeed, indentLevel } = {}
{ tab, author, description, keywords, timeUTC, timeLocal, delimiter, indentLevel } = {}
) {
return PlaceHolderParser.process(format, (name, rawArgs, ...args) => {
return processPlaceHolder(
name,
rawArgs,
args,
{ tab, author, description, keywords, timeUTC, timeLocal, lineFeed, indentLevel }
{ tab, author, description, keywords, timeUTC, timeLocal, delimiter, indentLevel }
);
}, '', log);
}
Expand All @@ -292,7 +311,7 @@ function processPlaceHolder(
name,
rawArgs,
args,
{ tab, author, description, keywords, timeUTC, timeLocal, lineFeed, indentLevel } = {}
{ tab, author, description, keywords, timeUTC, timeLocal, delimiter, indentLevel } = {}
) {
log('processPlaceHolder ', name, rawArgs, args);
switch (name.trim().toLowerCase()) {
Expand Down Expand Up @@ -361,7 +380,7 @@ function processPlaceHolder(
return '\t';

case 'eol':
return lineFeed;
return getLineFeed();

case 'tst_indent': {
const indenters = args.length == 0 ?
Expand All @@ -384,7 +403,7 @@ function processPlaceHolder(
matchedToHTMLSafe[1],
rawArgs,
args,
{ tab, author, description, keywords, timeUTC, timeLocal, lineFeed, indentLevel }
{ tab, author, description, keywords, timeUTC, timeLocal, delimiter, indentLevel }
));

return rawArgs ? `%${name}(${rawArgs})%` : `%${name}%`;
Expand Down
1 change: 1 addition & 0 deletions common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const configs = new Configs({
shouldNotifyResult: true,
copyToClipboardFormats: defaultClipboardFormats,
reportErrors: false,
delimiter: Constants.kDELIMITER_LINE_BREAK,
useCRLF: false,
notificationTimeout: 10 * 1000,
debug: false,
Expand Down
4 changes: 4 additions & 0 deletions common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const kCOPY_TREE_DESCENDANTS = 3;
export const kCOPY_ALL = 4;
export const kCOPY_CHOOSE_FROM_MENU = 5;

export const kDELIMITER_LINE_BREAK = 0;
export const kDELIMITER_SPACE = 1;
export const kDELIMITER_TAB = 2;

export const WITH_CONTAINER_MATCHER = /%(?:CONTAINER_URL(?:_HTML(?:IFIED)?)?|CONTAINER_(?:TITLE|NAME)(?:_HTML(?:IFIED)?)?(?:\(.*?\))?)%/i;


Expand Down
18 changes: 18 additions & 0 deletions options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@

</fieldset>

<fieldset>
<legend>__MSG_config_delimiter_caption__</legend>
<ul>
<li><label><input name="delimiter"
value="0"
type="radio"
>__MSG_config_delimiter_lineBreak_label__</label></li>
<li><label><input name="delimiter"
value="1"
type="radio"
>__MSG_config_delimiter_space_label__</label></li>
<li><label><input name="delimiter"
value="2"
type="radio"
>__MSG_config_delimiter_tab_label__</label></li>
</ul>
</fieldset>

<div class="grid" id="copyToClipboardFormatsRows">
</div>
<p><button id="copyToClipboardFormatsAddNewRow"
Expand Down

0 comments on commit 9240be8

Please sign in to comment.