JavaScript - 用于 Keil IDE中,格式化宏定义,在行末添加行连接符

<!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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值