Skip to content

Commit 217ff27

Browse files
authored
Merge pull request #1008 from MaterArc/fix-align
Fix Align to Center
2 parents 9b6db3c + ca49026 commit 217ff27

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

features/align-to-center/script.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
export default async function ({ feature, console }) {
22
const availableWidth = 405;
3+
const blankChar = "\u2800";
34

45
function getSpaceWidth() {
56
const span = document.createElement("span");
67
span.style.visibility = "hidden";
78
span.style.whiteSpace = "pre";
8-
span.textContent = " ";
9+
span.textContent = blankChar;
910
document.body.appendChild(span);
1011
const spaceWidth = span.getBoundingClientRect().width;
1112
document.body.removeChild(span);
@@ -26,7 +27,7 @@ export default async function ({ feature, console }) {
2627
function clearCenterAlignment(textarea) {
2728
const lines = textarea.value.split("\n");
2829
const uncenteredLines = lines.map((line) => {
29-
return line.replace(/^\s+/, "");
30+
return line.replace(/^[\s\u2800]+/, "");
3031
});
3132
textarea.value = uncenteredLines.join("\n");
3233
}
@@ -49,7 +50,7 @@ export default async function ({ feature, console }) {
4950
const textWidth = getTextWidth(line);
5051
const totalSpaces = (availableWidth - textWidth) / spaceWidth / 2;
5152
const spaces =
52-
totalSpaces > 0 ? " ".repeat(Math.floor(totalSpaces)) : "";
53+
totalSpaces > 0 ? blankChar.repeat(Math.floor(totalSpaces)) : "";
5354
return spaces + line;
5455
});
5556
activeElement.value = centeredLines.join("\n");

0 commit comments

Comments
 (0)