<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Keil Format</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font: "courier new";
}
body {
width: 100%;
height: 100vh;
}
.container {
background-color: gray;
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
padding: 1px;
}
textarea {
--margin: 1px;
width: 100%;
height: calc(100% - (var(--margin) * 2));
outline: none;
resize: none;
background-color: antiquewhite;
margin: var(--margin);
padding: 5px;
border: none;
}
</style>
</head>
<body>
<div class="container">
<textarea id="editor"></textarea>
<textarea id="previewer"></textarea>
</div>
<script type="text/javascript">
window.onload = (event) => {
// console.log(event)
main()
}
function main() {
const editor = document.getElementById("editor")
const previewer = document.getElementById("previewer")
editor.oninput = (event) => {
console.log(event)
const originalContent = editor.value;
previewer.value = ""
const lines = originalContent.split("\n");
console.log(`Total lines: ${lines.length}`);
console.log(lines);
// 格式处理
let targetLength = 0;
for(let i = 0; i < lines.length; i++) {
// console.log(lines[i].charAt(lines[i].length - 1));
if(lines[i].charAt(lines[i].length - 1) === "\\") { // 行末是否包含 \ 字符
// console.log(lines[i].slice(0, -1))
lines[i] = lines[i].slice(0, -1).trimRight(); // 删除行末的空格和 \ 字符
}
if(lines[i].length > targetLength) {
targetLength = lines[i].length;
}
}
// console.log("targetLength (old) = " + targetLength);
targetLength += (4 - (targetLength % 4));
// console.log("targetLength (new) = " + targetLength);
for(let i = 0; i < lines.length; i++) {
if(i < (lines.length - 1)) {
if(lines[i].length < targetLength) {
lines[i] = lines[i].padEnd(targetLength, " "); // 行末填充指定长度的空格
}
lines[i] += ("\\" + "\n");
}
previewer.value += lines[i];
}
}
editor.onscroll = (event) => {
// console.log(event)
const target = event.target
console.log(target.scrollHeight, target.scrollTop)
previewer.scrollTop = target.scrollTop
}
editor.ondblclick = (event) => {
// console.log(event)
event.target.select()
}
previewer.onscroll = (event) => {
// console.log(event)
const target = event.target
console.log(target.scrollHeight, target.scrollTop)
editor.scrollTop = target.scrollTop
}
previewer.ondblclick = (event) => {
// console.log(event)
event.target.select()
const content = event.target.value
// 写文本内容到系统剪贴板(相当于执行“复制”操作)
navigator.clipboard.writeText(content).then(function() {
/* clipboard successfully set */
console.log("")
}, function(error) {
/* clipboard write failed */
console.log(error)
})
}
}
</script>
</body>
</html>
JavaScript - 用于 Keil IDE中,格式化宏定义,在行末添加行连接符
最新推荐文章于 2023-12-26 18:52:29 发布