简体   繁体   English

更漂亮:在 HTML/JSX HTML 中保留分隔属性的空行

[英]Prettier: Keeping empty lines separating attributes in HTML/JSX HTML

So I've been trying to find a way to tell Prettier to keep line breaks between attributes in HTML, for keeping code clear, but I'm coming up empty.所以我一直在试图找到一种方法来告诉 Prettier 在 HTML 中的属性之间保留换行符,以保持代码清晰,但我的结果是空的。 I'm working in TypeScript React with Styled Components, don't know if that makes a difference for the answer.我在 TypeScript React with Styled Components 工作,不知道这是否对答案有影响。 I've been wondering if this is a job for Beautify, but since I'm using prettier to auto-format files on commit I'm worried using both might get very messy.我一直想知道这是否是 Beautify 的工作,但由于我在提交时使用 prettier 来自动格式化文件,我担心同时使用两者可能会变得非常混乱。

Here is what I'm talking about:这就是我所说的:

 <CustomInput value={whatever} width="100px" height="32px" background="#333" borderColor="sandybrown" onFocus={handleFocus} onBlur={handleBlur} onChange={evt => setWhatever(evt.target.value)} />

Notice the empty lines separating the different "categories" of attributes.注意分隔不同“类别”属性的空行。 Prettier automatically removes these. Prettier 会自动删除这些。 Is this possible to achieve?这有可能实现吗?

Prettifiers aught to remove empty lines in my opinion在我看来,美化者可以删除空行

Perhaps you can use HTML comments也许您可以使用 HTML 注释

 <CustomInput value="{whatever}" <.-- formatting --> width="100px" height="32px" background="#333" borderColor="sandybrown" <.-- event handlers--> onFocus={handleFocus} onBlur={handleBlur} onChange={evt => setWhatever(evt.target.value)} />

Or just do what is recommended:或者只是做推荐的事情:

 const handleFocus = () => {} const handleBlur = () => {} window.addEventListener("load", function() { const inp = document.querySelector("CustomInput"); inp.addEventListener("focus", handleFocus); inp.addEventListener("blur", handleBlur); inp.addEventListener("change", evt => setWhatever(evt.target.value)); });
 CustomInput { display: block; width: 100px; height: 32px; background: #333; border-color: sandybrown; color: white; }
 <CustomInput value="{whatever}">Text</CustomInput>

It's indeed supposed to work the way you expect (just like it works in object literals).它确实应该按照您期望的方式工作(就像它在 object 文字中工作一样)。 This is simply not implemented yet.这根本还没有实现。 There is an open pull request , but it seems to be abandoned.一个 open pull request ,但它似乎被放弃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM