CodeMirror(JS代码高亮)textarea中的文本超出textarea宽度

发布于 2024-11-05 08:44:28 字数 827 浏览 0 评论 0原文

我今天一直在使用 CodeMirror 创建一个小环境,在其中我可以编辑存储在数据库中的一些 PHP 代码(是的,我知道这可能是有害的,但普通用户无法访问 PHP 代码)。

一切都很好,编辑器可以工作,代码突出显示可以工作,缩进选项卡可以工作,但是有一件事困扰了我一段时间,我似乎找不到解决方案。我的 CodeMirror 编辑器文本区域中的代码比文本区域长,超出了文本区域,并将消失在屏幕之外的某个位置(请参阅本文末尾的屏幕截图)。

我想让这段代码继续在下面一行(当然不添加额外的行号)。这是一个已知问题和/或容易修复吗?

这是一个屏幕截图: http://www.pendemo.nl/codemirror.png

提前致谢。

//编辑:已修复

好吧,搞清楚了,似乎都在 CSS 文件中!这是任何感兴趣的人的修复:

.CodeMirror {
  overflow-y: auto;
  overflow-x: scroll;
  width: 700px;
  height: auto;
  line-height: 1em;
  font-family: monospace;
  _position: relative; /* IE6 hack */
}

overflow-y:auto(高度是自动完成的,因此不需要垂直滚动条)。溢出-x:滚动;强制 CodeMirror 添加滚动条而不是超出文本区域的宽度。它们给出固定的宽度(像素或百分比)。您还可以添加最大高度,但如果这样做,您可能必须设置溢出-y 才能滚动。

I've been working with CodeMirror today to create a little environment where I can edit some PHP code that is stored in a database (yes, I know this could be harmfull, but the PHP code is not accesable for normal users).

Everythings works great, the editor works, code highlighting works, indent tabs work, but there's one thing that's bothering me for a while now and I can't seem to find a solutions to this. The code that is within my CodeMirror editor textarea, which is longer than the textarea exceeds the textarea and will disappear somewhere out of my screen (see the screenshot at the end of this post).

I would like to have this code to continue on a line below (without adding an extra linenumber ofcourse). Is this a known issue and/or easy to fix?

Here's a screenshot:
http://www.pendemo.nl/codemirror.png

Thanks in advance.

//Edit: it's fixed

Ok, got it figured out, seemed to be all in the CSS file! Here's the fix for anyone that's interested:

.CodeMirror {
  overflow-y: auto;
  overflow-x: scroll;
  width: 700px;
  height: auto;
  line-height: 1em;
  font-family: monospace;
  _position: relative; /* IE6 hack */
}

overflow-y: auto (height is being done auto, so no need for a vertical scrollbar). overflow-x: scroll; to force CodeMirror to add a scrollbar instead of exceeding the width of the textarea. And they give a fixed width (px or percentage). You can also add a max-height, but if you do you perhaps have to set overflow-y to scroll aswel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半透明的墙 2024-11-12 08:44:28

通过将 lineWrapping 选项设置为 true,可以轻松在 CodeMirror 中启用自动换行。例子:

    <textarea id="code" name="code">
      ...
    </textarea>

    <script>
      var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        tabMode: "indent",
        matchBrackets: true,
        theme: "night",
        lineNumbers: true,
        lineWrapping: true,
        indentUnit: 4,
        mode: "text/javascript"
      });
    </script>

It's easy to enable word wrap in CodeMirror by setting the lineWrapping option to true. Example:

    <textarea id="code" name="code">
      ...
    </textarea>

    <script>
      var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        tabMode: "indent",
        matchBrackets: true,
        theme: "night",
        lineNumbers: true,
        lineWrapping: true,
        indentUnit: 4,
        mode: "text/javascript"
      });
    </script>
若沐 2024-11-12 08:44:28

它已修复,如果其他人可能遇到此问题,请参阅问题以获取详细信息。

It's fixed, see question for the details if anyone else might bump onto this problem.

十六岁半 2024-11-12 08:44:28

正如克里斯上面评论的那样,问题中描述的解决方案不再(总是?)起作用。

然而,向 CSS 添加 max-width: ...ex; 似乎确实会强制水平滚动条。

(如果没有这个,例如仅使用 width: ...,仅使用 lineWrapping: true 配置 CodeMirror (如 fywe 的答案)可以防止它公然拉伸盒子很长时间线,但 lineWrapping 有其自身的缺点(例如导航不友好的 Home/End 处理),因此这可能没有真正的帮助。)

As Chris commented above, the solution described in the question doesn't (always?) work any more.

However, adding max-width: ...ex; to CSS does seem to force a horizontal scrollbar.

(Without that, e.g. just using width: ..., only configuring CodeMirror with lineWrapping: true (as in fywe's answer) could prevent it from blatantly stretching the box for long lines, but lineWrapping has its own drawbacks (e.g. navigation-unfriendly Home/End handling), so that may not really help.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文