This commit is contained in:
parent
40e5379eb3
commit
de63c48da2
9 changed files with 415 additions and 1 deletions
44
site/queued-pastes/queue.mjs
Normal file
44
site/queued-pastes/queue.mjs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
// Pure queue logic for the Queued Pastes tool. Kept free of DOM and
|
||||
// clipboard APIs so it can be unit tested under Node (see queue.test.mjs).
|
||||
|
||||
// Returns the range of the first line, excluding its trailing newline.
|
||||
export function firstLineRange(text) {
|
||||
const newline = text.indexOf('\n');
|
||||
return { start: 0, end: newline === -1 ? text.length : newline };
|
||||
}
|
||||
|
||||
// Returns the first line of text, without its trailing newline.
|
||||
export function currentLine(text) {
|
||||
const { start, end } = firstLineRange(text);
|
||||
return text.slice(start, end);
|
||||
}
|
||||
|
||||
// Returns the text with the first line (and its trailing newline) removed.
|
||||
export function advance(text) {
|
||||
const newline = text.indexOf('\n');
|
||||
return newline === -1 ? '' : text.slice(newline + 1);
|
||||
}
|
||||
|
||||
// Returns the text to copy for the first line, honoring the
|
||||
// include-new-line setting.
|
||||
export function copyText(text, includeNewline) {
|
||||
const line = currentLine(text);
|
||||
return includeNewline ? line + '\n' : line;
|
||||
}
|
||||
|
||||
// Returns the clipboard contents adjusted for a toggled include-new-line
|
||||
// setting, or null if the clipboard does not currently hold the given line
|
||||
// (in which case it must be left untouched).
|
||||
export function adjustCopiedLine(clipboardText, line, includeNewline) {
|
||||
const stripped = clipboardText.endsWith('\n') ? clipboardText.slice(0, -1) : clipboardText;
|
||||
if (stripped !== line) {
|
||||
return null;
|
||||
}
|
||||
return includeNewline ? line + '\n' : line;
|
||||
}
|
||||
|
||||
// Removes trailing whitespace (spaces, tabs, newlines) from the end of the
|
||||
// text, so enabling the queue does not leave phantom trailing lines.
|
||||
export function trimTrailing(text) {
|
||||
return text.replace(/\s+$/, '');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue