Skip to content

Commit

Permalink
Feat--Editor-copy-cut-line (#9)
Browse files Browse the repository at this point in the history
- adds the ability to copy/cut a line if nothing is highlighted
  • Loading branch information
rossrobino authored Oct 6, 2023
1 parent 034dfff commit d12b79b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
Binary file modified bun.lockb
Binary file not shown.
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drab",
"version": "4.1.2",
"version": "4.1.3",
"description": "An Unstyled Svelte Component Library",
"keywords": [
"components",
Expand Down Expand Up @@ -50,35 +50,35 @@
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --check . && eslint .",
"format": "prettier --write . --plugin=prettier-plugin-svelte --plugin=prettier-plugin-tailwindcss",
"format": "prettier --write .",
"pub": "bun i && bun doc && bun package && npm publish --access public",
"doc": "bun format && node src/scripts/documentProps.js && node src/scripts/documentExamples.js && node src/scripts/copyReadMe.js"
},
"dependencies": {
"svelte": "^4.2.0"
"svelte": "^4.2.1"
},
"devDependencies": {
"@sveltejs/adapter-vercel": "^3.0.3",
"@sveltejs/kit": "^1.25.0",
"@sveltejs/kit": "^1.25.1",
"@sveltejs/package": "^2.2.2",
"@tailwindcss/typography": "^0.5.10",
"@types/node": "^20.6.1",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"autoprefixer": "^10.4.15",
"eslint": "^8.49.0",
"@types/node": "^20.8.2",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"autoprefixer": "^10.4.16",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-svelte": "^2.33.1",
"eslint-plugin-svelte": "^2.34.0",
"prettier": "^3.0.3",
"prettier-plugin-svelte": "^3.0.3",
"prettier-plugin-tailwindcss": "^0.5.4",
"publint": "^0.2.2",
"robino": "^0.0.19",
"svelte-check": "^3.5.1",
"prettier-plugin-tailwindcss": "^0.5.5",
"publint": "^0.2.3",
"robino": "^0.0.39",
"svelte-check": "^3.5.2",
"tailwindcss": "^3.3.3",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"uico": "^0.1.4",
"vite": "^4.4.9"
"uico": "^0.1.5",
"vite": "^4.4.11"
}
}
24 changes: 24 additions & 0 deletions src/lib/components/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
* @param keyboardEvent KeyboardEvent
*/
const onKeyDown = async (e: KeyboardEvent) => {
/** these keys will reset the type over for characters like " */
const resetKeys = ["ArrowUp", "ArrowDown", "Delete"];
const nextChar = valueTextarea[textArea.selectionEnd];
if (resetKeys.includes(e.key)) {
Expand All @@ -278,6 +279,7 @@
} else if (e.key === "Backspace") {
const prevChar = valueTextarea[textArea.selectionStart - 1];
if (prevChar in keyPairs && nextChar === keyPairs[prevChar]) {
// remove both characters if the next one is the match of the prev
e.preventDefault();
const start = textArea.selectionStart - 1;
const end = textArea.selectionEnd - 1;
Expand All @@ -292,6 +294,7 @@
prevChar === "\n" &&
textArea.selectionStart === textArea.selectionEnd
) {
// see `correctFollowing`
e.preventDefault();
const newPos = textArea.selectionStart - 1;
const { lineNumber } = getLineInfo();
Expand Down Expand Up @@ -354,6 +357,27 @@
});
}, 0);
}
} else if (e.ctrlKey || e.metaKey) {
if (textArea.selectionStart === textArea.selectionEnd) {
if (e.key === "c" || e.key === "x") {
// copy or cut entire line
e.preventDefault();
const { lines, lineNumber, columnNumber } = getLineInfo();
await navigator.clipboard.writeText(
`${lineNumber === 0 && e.key === "x" ? "" : "\n"}${
lines[lineNumber]
}`,
);
if (e.key === "x") {
const newPos = textArea.selectionStart - columnNumber;
lines.splice(lineNumber, 1);
valueTextarea = lines.join("\n");
setTimeout(() => {
textArea.setSelectionRange(newPos, newPos);
}, 0);
}
}
}
} else {
const nextCharIsClosing = Object.values(keyPairs).includes(nextChar);
const highlighted = textArea.selectionStart !== textArea.selectionEnd;
Expand Down

1 comment on commit d12b79b

@vercel
Copy link

@vercel vercel bot commented on d12b79b Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.