ui: editor auto-resize

This commit is contained in:
undefined committed 2021-05-05 18:10:18 +08:00
1 parent 0c5f941203
commit 6c0c2c5bf1
4 files changed
+42 -17

No files matched your search

+37 -13
View File
@@ -40,7 +40,6 @@ export default class Editor extends DOMAttachedObject {
const hasFocus = $dom.is(':focus'); const hasFocus = $dom.is(':focus');
const origin = $dom.get(0); const origin = $dom.get(0);
const ele = document.createElement('div'); const ele = document.createElement('div');
$(ele).height(550);
$(ele).width('100%'); $(ele).width('100%');
$(ele).addClass('textbox'); $(ele).addClass('textbox');
$dom.hide(); $dom.hide();
@@ -50,20 +49,25 @@ export default class Editor extends DOMAttachedObject {
onChange, language = 'markdown', onChange, language = 'markdown',
theme = UserContext.monacoTheme || 'vs-light', theme = UserContext.monacoTheme || 'vs-light',
model = `file://model-${Math.random().toString(16)}`, model = `file://model-${Math.random().toString(16)}`,
autoResize = $dom.hasClass('auto-resize'),
} = this.options; } = this.options;
this.model = typeof model === 'string' ? monaco.editor.createModel(value, language, monaco.Uri.parse(model)) : model; this.model = typeof model === 'string' ? monaco.editor.createModel(value, language, monaco.Uri.parse(model)) : model;
this.editor = monaco.editor.create( const cfg = {
ele, theme,
{ lineNumbers: true,
theme, glyphMargin: true,
lineNumbers: true, lightbulb: { enabled: true },
glyphMargin: true, model: this.model,
lightbulb: { automaticLayout: true,
enabled: true, };
}, if (autoResize) {
model: this.model, cfg.scrollbar = {
} vertical: 'hidden',
); handleMouseWheel: false,
};
cfg.scrollBeyondLastLine = false;
}
this.editor = monaco.editor.create(ele, cfg);
this.editor.addAction({ this.editor.addAction({
id: 'theme-dark', id: 'theme-dark',
label: 'Use dark theme', label: 'Use dark theme',
@@ -74,6 +78,26 @@ export default class Editor extends DOMAttachedObject {
label: 'Use light theme', label: 'Use light theme',
run: () => monaco.editor.setTheme('vs-light'), run: () => monaco.editor.setTheme('vs-light'),
}); });
let prevHeight = 0;
const updateEditorHeight = () => {
const editorElement = this.editor.getDomNode();
if (!editorElement) return;
const lineHeight = this.editor.getOption(monaco.editor.EditorOption.lineHeight);
const lineCount = this.editor.getModel()?.getLineCount() || 1;
const height = this.editor.getTopForLineNumber(lineCount + 1) + lineHeight;
if (prevHeight !== height) {
prevHeight = height;
editorElement.style.height = `${height}px`;
this.editor.layout();
}
};
this.editor.onDidChangeModelDecorations(() => {
updateEditorHeight(); // typing
requestAnimationFrame(updateEditorHeight); // folding
});
this._subscription = this.editor.onDidChangeModelContent(() => { this._subscription = this.editor.onDidChangeModelContent(() => {
const val = this.editor.getValue(); const val = this.editor.getValue();
$dom.val(val); $dom.val(val);
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/ui-default", "name": "@hydrooj/ui-default",
"version": "4.6.32", "version": "4.6.33",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"main": "hydro.js", "main": "hydro.js",
@@ -42,5 +42,6 @@
hotkeys:'ctrl+enter:submit', hotkeys:'ctrl+enter:submit',
markdown:true, markdown:true,
required:true, required:true,
extra_style:'height: 500px' extra_style:'height: 500px',
extra_class:'auto-resize'
}) }} }) }}
@@ -9,7 +9,7 @@
<div class="medium-7 columns"> <div class="medium-7 columns">
<label> <label>
{{ _('Title') }} {{ _('Title') }}
<input name="title" placeholder="{{ _('title') }}" value="{{ pdoc['title']|default('') }}" class="textbox" autofocus> <input name="title" placeholder="{{ _('title') }}" value="{{ pdoc['title']|default('') }}" class="textbox"{% if page_name == 'problem_create' %} autofocus{% endif %}>
</label> </label>
</div> </div>
<div class="medium-3 columns"> <div class="medium-3 columns">
@@ -49,7 +49,7 @@
</ul> </ul>
</div> </div>
</div> </div>
<div><textarea data-editor class="nojs--hide"></textarea></div> <div><textarea data-editor{% if page_name == 'problem_edit' %} autofocus{% endif %} class="textbox auto-resize nojs--hide"></textarea></div>
<textarea name="content" class="textbox hasjs--hide" data-markdown-upload style="height: 500px">{% if pdoc %}{{ pdoc['content']|toString }}{% else %}{% include 'partials/problem_default.md' %}{% endif %}</textarea> <textarea name="content" class="textbox hasjs--hide" data-markdown-upload style="height: 500px">{% if pdoc %}{{ pdoc['content']|toString }}{% else %}{% include 'partials/problem_default.md' %}{% endif %}</textarea>
<div class="row"><div class="columns"> <div class="row"><div class="columns">
<input type="hidden" name="csrfToken" value="{{ handler.csrfToken }}"> <input type="hidden" name="csrfToken" value="{{ handler.csrfToken }}">