问题 如何让ace编辑器调整到其父div


我在另一个div中有ace div,我希望ace编辑器将它的宽度和高度调整为父div。我调用editor.resize()但没有任何反应。

<!DOCTYPE html>
<html lang="en" style="height: 100%">
<head>
<title>ACE in Action</title>
<style type="text/css" media="screen">
    #editor { 
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        height: 100px;
    }
</style>
</head>
<body style="height: 100%">
<div style="background-color: red; height: 100%; width: 100%;">
<div id="editor">function foo(items) {
    var x = "All this is syntax highlighted";
    return x;
}</div>
</div>

<script src="ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/monokai");
    editor.getSession().setMode("ace/mode/javascript");

    editor.resize();
</script>
</body>
</html>

1175
2018-02-28 18:59


起源



答案:


你可以用两种方式达到你想要的效果。我创造了一个 的jsfiddle 显示用于将ace编辑器的大小调整为其容器的css和javascript。

使用的css是为了使编辑器占用容器的宽度和高度,这样做 editor.resize() 可以正确计算编辑器的大小。

我推荐以下内容来获取 editor.resize() 上班。

<style type="text/css" media="screen">
    #editor {
        width: 100%;
        height: 100%;
    }
</style>

但是,如果您想维护使用当前的css #editor 以下将有效。

<style type="text/css" media="screen">
    #editor {
        position: absolute; /* Added */
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
   }
</style>

并添加 position: relative; 到容器,以便绝对定位的编辑器正确定位在其容器内。至于这是如何工作的,我推荐你 绝对定位内部相对定位。


8
2017-09-24 01:05



您的逻辑调整高度而不是宽度。对? - Ashit Vora
应调整高度和宽度。这就是高度和宽度的100%。 - scain


答案:


你可以用两种方式达到你想要的效果。我创造了一个 的jsfiddle 显示用于将ace编辑器的大小调整为其容器的css和javascript。

使用的css是为了使编辑器占用容器的宽度和高度,这样做 editor.resize() 可以正确计算编辑器的大小。

我推荐以下内容来获取 editor.resize() 上班。

<style type="text/css" media="screen">
    #editor {
        width: 100%;
        height: 100%;
    }
</style>

但是,如果您想维护使用当前的css #editor 以下将有效。

<style type="text/css" media="screen">
    #editor {
        position: absolute; /* Added */
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
   }
</style>

并添加 position: relative; 到容器,以便绝对定位的编辑器正确定位在其容器内。至于这是如何工作的,我推荐你 绝对定位内部相对定位。


8
2017-09-24 01:05



您的逻辑调整高度而不是宽度。对? - Ashit Vora
应调整高度和宽度。这就是高度和宽度的100%。 - scain


运用 jQuery的王牌 我通过使用简单地管理了这个:

    $('#php_code').ace({
        theme: 'chrome',
        lang: 'php',
        width: '100%',
        height: '300px'
    })

2
2017-07-17 02:19



你可以不用jquery以同样的方式做到这一点,只需设置 width 至 '100%' - Roman Golyshev


您可以通过以下方式实现。例如,运行代码段。

var editor = ace.edit("editor");
        editor.setTheme("ace/theme/tomorrow_night");
        editor.session.setMode("ace/mode/xml");
        editor.session.setUseSoftTabs(true);
#parent {
    width:50%;
    height: 600px;
    display:inline-block;
    position:relative;
}
#editor {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.3/ace.js"></script>
<html>
   <body>
      <div id="parent">
          <div id="editor"></div>
      </div>
   </body>
</html>


1
2017-07-16 11:39





我使用简单的CSS:

#container{
    height:80vh;
}

#editor {
    width: 100%;
    height: 100%;
    position: relative;
}

关键的css属性是 position:relative,凌驾于违约之上 position:absolute ace编辑器,它生成父容器无法调整其内容。

<div id="container">
    <pre id="editor">
        &#x3C;div&#x3E;
        &#x9;&#x9;this is a div
        &#x9;&#x3C;/div&#x3E;
    </pre>
</div>

<script>
    $(document).ready(function() {
        var editor = ace.edit("editor");
        editor.setTheme("ace/theme/TextMate");
        editor.session.setMode("ace/mode/html");
    });
</script>

1
2017-08-10 09:00





将其设置重置为浏览器默认设置,根据设计适合父容器。

#editor {
    width: inherit !important;
}

我正在使用react-ace包装器来反应。 对于任何覆盖了一些默认值的ace包装器可能是有益的。


0
2017-09-05 13:38