简体   繁体   English

1行高的CodeMirror实例

[英]1 line high CodeMirror instance

I'm writing a program that will use a CodeMirror instance and have a little element that pops up that you can, among other things, type text in. That text typing area is supposed to be one line high. 我正在编写一个程序,该程序将使用CodeMirror实例并弹出一个小元素,您可以在其中输入文本。该文本输入区域应该高一行。 I'm going to be doing a lot of the same stuff with that text typing area that I'm doing with the main CodeMirror instance, so I want to just use another instance of CodeMirror, but everything I've tried so far ends up too tall. 我将在与主CodeMirror实例相同的文本键入区域中做很多相同的事情,所以我只想使用CodeMirror的另一个实例,但是到目前为止我尝试过的一切都结束了太高了。

How do I make a CodeMirror instance that is just one line high and is horizontally scrollable? 如何制作仅一行高且可水平滚动的CodeMirror实例? I'd like no line numbers, no gutters, etc., just the area for the text to be entered. 我想要没有行号,没有装订线等,仅是要输入文本的区域。

I tried several things including the code here (which I tried in whole and in parts): codemirror for just one-line-textfield? 我尝试了几件事,包括此处的代码(全部或部分尝试了): 仅用于一行文本字段的codemirror? . The example prevents a user from typing more than one line of code in a code mirror instance, but it doesn't make it just one line high. 该示例防止用户在代码镜像实例中键入多行代码,但并不仅限于此。 There's other CodeMirror stuff there, though I'm not sure what all is there or how to get rid of it. 那里还有其他CodeMirror东西,尽管我不确定那里到底有什么或如何摆脱它。

Edit: re: @rfornal's request, here was the code and explanation I'm referring to (by Tigran Saluev): 编辑:回复:@rfornal的请求,这是我指的代码和解释(作者Tigran Saluev):

Well, there is a way to make a single-line editor using rich capabilities of CodeMirror. 嗯,有一种方法可以使用CodeMirror的丰富功能来制作单行编辑器。 First, you'll have to add a full-featured CodeMirror object (use a textarea). 首先,您必须添加功能齐全的CodeMirror对象(使用文本区域)。

Assume you've got var cm = CodeMirror(...). 假设您有var cm = CodeMirror(...)。 (Use value: ""). (使用值:“”)。 Then do 然后做

 cm.setSize(200, cm.defaultTextHeight() + 2 * 4); // 200 is the preferable width of text field in pixels, // 4 is default CM padding (which depends on the theme you're using) // now disallow adding newlines in the following simple way cm.on("beforeChange", function(instance, change) { var newtext = change.text.join("").replace(/\\n/g, ""); // remove ALL \\n ! change.update(change.from, change.to, [newtext]); return true; }); // and then hide ugly horizontal scrollbar cm.on("change", function(instance, change) { $(".CodeMirror-hscrollbar").css('display', 'none'); // (!) this code is using jQuery and the selector is quite imperfect if // you're using more than one CodeMirror on your page. you're free to // change it appealing to your page structure. }); // the following line fixes a bug I've encountered in CodeMirror 3.1 $(".CodeMirror-scroll").css('overflow', 'hidden'); // jQuery again! be careful with selector or move this to .css file 

This works just fine for me. 这对我来说很好。

Everything I've tried so far still ends up taller than one line. 到目前为止,我尝试过的所有内容仍然比一行高。 There's probably a way to do this, I'm just not understanding how. 可能有一种方法可以做到,我只是不了解如何。

As per usual, this was user error. 像往常一样,这是用户错误。 I had assumed that CodeMirror's styles would overwrite any styles that I had created for the container, but they didn't. 我以为CodeMirror的样式会覆盖我为容器创建的任何样式,但事实并非如此。 My styles infected the CodeMirror instance and caused the weirdness. 我的样式感染了CodeMirror实例并引起了怪异。

Specifically: 特别:
- Setting position: relative somewhere where it shouldn't be (I'm not sure where) -设置位置:相对位置不应该的位置(我不确定该在哪里)
- Setting display: inline-block somewhere where it shouldn't be (again, not sure what it specifically affected) -设置显示:在不应该出现的地方进行内联阻止(再次,不确定具体影响了什么)

My apologies and I hope this helps someone else in the future. 我深表歉意,希望以后对其他人有帮助。

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

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