mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: monaco: better auto-resize
This commit is contained in:
1 parent
6419215d91
commit
6b5b3e68d0
4 files changed
+62
-16
No files matched your search
+45
-15
@@ -24,12 +24,29 @@ export const config = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface MonacoOptions {
|
||||||
|
language?: string;
|
||||||
|
onChange?: (val: string) => any;
|
||||||
|
theme?: string;
|
||||||
|
model?: string;
|
||||||
|
autoResize?: boolean;
|
||||||
|
autoLayout?: boolean;
|
||||||
|
value?: string;
|
||||||
|
}
|
||||||
|
interface VditorOptions {
|
||||||
|
theme?: 'classic' | 'dark'
|
||||||
|
}
|
||||||
|
type Options = MonacoOptions & VditorOptions;
|
||||||
|
|
||||||
export default class Editor extends DOMAttachedObject {
|
export default class Editor extends DOMAttachedObject {
|
||||||
static DOMAttachKey = 'vjEditorInstance';
|
static DOMAttachKey = 'vjEditorInstance';
|
||||||
|
model: import('../monaco').default.editor.IModel;
|
||||||
|
editor: import('../monaco').default.editor.IStandaloneCodeEditor;
|
||||||
|
vditor: import('vditor').default;
|
||||||
|
isValid: boolean;
|
||||||
|
|
||||||
constructor($dom, options = {}) {
|
constructor($dom, public options: Options = {}) {
|
||||||
super($dom);
|
super($dom);
|
||||||
this.options = options;
|
|
||||||
if (UserContext.preferredEditorType === 'monaco') this.initMonaco();
|
if (UserContext.preferredEditorType === 'monaco') this.initMonaco();
|
||||||
else if (options.language && options.language !== 'markdown') this.initMonaco();
|
else if (options.language && options.language !== 'markdown') this.initMonaco();
|
||||||
else this.initVditor();
|
else this.initVditor();
|
||||||
@@ -59,10 +76,9 @@ export default class Editor extends DOMAttachedObject {
|
|||||||
|| monaco.editor.createModel(value, language, monaco.Uri.parse(model))
|
|| monaco.editor.createModel(value, language, monaco.Uri.parse(model))
|
||||||
: model;
|
: model;
|
||||||
this.model.setValue(value);
|
this.model.setValue(value);
|
||||||
this.model.updateOptions({ language });
|
const cfg: import('../monaco').default.editor.IStandaloneEditorConstructionOptions = {
|
||||||
const cfg = {
|
|
||||||
theme,
|
theme,
|
||||||
lineNumbers: true,
|
lineNumbers: 'on',
|
||||||
glyphMargin: true,
|
glyphMargin: true,
|
||||||
lightbulb: { enabled: true },
|
lightbulb: { enabled: true },
|
||||||
model: this.model,
|
model: this.model,
|
||||||
@@ -79,19 +95,31 @@ export default class Editor extends DOMAttachedObject {
|
|||||||
const lineHeight = this.editor.getOption(monaco.editor.EditorOption.lineHeight);
|
const lineHeight = this.editor.getOption(monaco.editor.EditorOption.lineHeight);
|
||||||
const lineCount = this.editor.getModel()?.getLineCount() || 1;
|
const lineCount = this.editor.getModel()?.getLineCount() || 1;
|
||||||
let height = this.editor.getTopForLineNumber(lineCount + 1) + lineHeight;
|
let height = this.editor.getTopForLineNumber(lineCount + 1) + lineHeight;
|
||||||
if (window.innerHeight < height) height = window.innerHeight;
|
|
||||||
if (prevHeight !== height) {
|
if (prevHeight !== height) {
|
||||||
|
if (window.innerHeight * 1.5 < height) {
|
||||||
|
height = window.innerHeight;
|
||||||
|
this.editor.updateOptions({
|
||||||
|
scrollbar: {
|
||||||
|
vertical: 'auto',
|
||||||
|
horizontal: 'auto',
|
||||||
|
handleMouseWheel: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.editor.updateOptions({
|
||||||
|
scrollbar: {
|
||||||
|
vertical: 'hidden',
|
||||||
|
horizontal: 'hidden',
|
||||||
|
handleMouseWheel: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
prevHeight = height;
|
prevHeight = height;
|
||||||
editorElement.style.height = `${height}px`;
|
editorElement.style.height = `${height}px`;
|
||||||
this.editor.layout();
|
this.editor.layout();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (autoResize) {
|
if (autoResize) {
|
||||||
cfg.scrollbar = {
|
|
||||||
vertical: 'hidden',
|
|
||||||
horizontal: 'hidden',
|
|
||||||
handleMouseWheel: false,
|
|
||||||
};
|
|
||||||
cfg.wordWrap = 'bounded';
|
cfg.wordWrap = 'bounded';
|
||||||
cfg.scrollBeyondLastLine = false;
|
cfg.scrollBeyondLastLine = false;
|
||||||
}
|
}
|
||||||
@@ -103,7 +131,7 @@ export default class Editor extends DOMAttachedObject {
|
|||||||
requestAnimationFrame(updateEditorHeight); // folding
|
requestAnimationFrame(updateEditorHeight); // folding
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
this._subscription = this.editor.onDidChangeModelContent(() => {
|
this.editor.onDidChangeModelContent(() => {
|
||||||
const val = this.editor.getValue();
|
const val = this.editor.getValue();
|
||||||
$dom.val(val);
|
$dom.val(val);
|
||||||
$dom.text(val);
|
$dom.text(val);
|
||||||
@@ -112,7 +140,9 @@ export default class Editor extends DOMAttachedObject {
|
|||||||
this.isValid = true;
|
this.isValid = true;
|
||||||
if (hasFocus) this.focus();
|
if (hasFocus) this.focus();
|
||||||
if (autoResize) updateEditorHeight();
|
if (autoResize) updateEditorHeight();
|
||||||
|
// @ts-ignore
|
||||||
window.model = this.model;
|
window.model = this.model;
|
||||||
|
// @ts-ignore
|
||||||
window.editor = this.editor;
|
window.editor = this.editor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,10 +155,10 @@ export default class Editor extends DOMAttachedObject {
|
|||||||
const value = $dom.val();
|
const value = $dom.val();
|
||||||
const { onChange } = this.options;
|
const { onChange } = this.options;
|
||||||
await new Promise((resolve) => {
|
await new Promise((resolve) => {
|
||||||
this.editor = new Vditor(ele, {
|
this.vditor = new Vditor(ele, {
|
||||||
...config,
|
...config,
|
||||||
...this.options,
|
...this.options,
|
||||||
after: resolve,
|
after: () => resolve(null),
|
||||||
input(v) {
|
input(v) {
|
||||||
$dom.val(v);
|
$dom.val(v);
|
||||||
$dom.text(v);
|
$dom.text(v);
|
||||||
@@ -147,7 +177,7 @@ export default class Editor extends DOMAttachedObject {
|
|||||||
|
|
||||||
destory() {
|
destory() {
|
||||||
this.detach();
|
this.detach();
|
||||||
if (this.editor?.destory) this.editor.destory();
|
if (this.vditor?.destroy) this.vditor.destroy();
|
||||||
else if (this.editor?.dispose) this.editor.dispose();
|
else if (this.editor?.dispose) this.editor.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/ui-default",
|
"name": "@hydrooj/ui-default",
|
||||||
"version": "4.30.1",
|
"version": "4.30.2",
|
||||||
"author": "undefined <i@undefined.moe>",
|
"author": "undefined <i@undefined.moe>",
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"main": "hydro.js",
|
"main": "hydro.js",
|
||||||
|
|||||||
@@ -50,9 +50,11 @@ const page = new NamedPage('domain_group', () => {
|
|||||||
createGroupDialogContent.find('[name="create_group_users"]'),
|
createGroupDialogContent.find('[name="create_group_users"]'),
|
||||||
{ multi: true, height: 'auto' },
|
{ multi: true, height: 'auto' },
|
||||||
);
|
);
|
||||||
|
const targets = {};
|
||||||
$('input[data-gid]').get().forEach((ele) => {
|
$('input[data-gid]').get().forEach((ele) => {
|
||||||
const input = UserSelectAutoComplete.getOrConstruct<UserSelectAutoComplete<true>>($(ele), { multi: true, height: 'auto' });
|
const input = UserSelectAutoComplete.getOrConstruct<UserSelectAutoComplete<true>>($(ele), { multi: true, height: 'auto' });
|
||||||
const gid = ele.getAttribute('data-gid');
|
const gid = ele.getAttribute('data-gid');
|
||||||
|
targets[gid] = input;
|
||||||
let loaded = false;
|
let loaded = false;
|
||||||
input.onChange(() => {
|
input.onChange(() => {
|
||||||
if (input.value().length && !loaded) {
|
if (input.value().length && !loaded) {
|
||||||
@@ -123,8 +125,21 @@ const page = new NamedPage('domain_group', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleClickSaveAll() {
|
||||||
|
for (const gid of Object.keys(targets)) {
|
||||||
|
const uids = targets[gid].value();
|
||||||
|
try {
|
||||||
|
await update(gid, uids);
|
||||||
|
} catch (error) {
|
||||||
|
Notification.error(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Notification.success(i18n('Saved.'));
|
||||||
|
}
|
||||||
|
|
||||||
$('[name="create_group"]').click(() => handleClickCreateGroup());
|
$('[name="create_group"]').click(() => handleClickCreateGroup());
|
||||||
$('[name="delete_selected"]').click(() => handleClickDeleteSelected());
|
$('[name="delete_selected"]').click(() => handleClickDeleteSelected());
|
||||||
|
$('[name="save_all"]').on('click', () => handleClickSaveAll());
|
||||||
});
|
});
|
||||||
|
|
||||||
export default page;
|
export default page;
|
||||||
@@ -43,6 +43,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="section__body">
|
<div class="section__body">
|
||||||
<button class="rounded button" name="remove_selected">{{ _('Remove Selected Group') }}</button>
|
<button class="rounded button" name="remove_selected">{{ _('Remove Selected Group') }}</button>
|
||||||
|
<button class="rounded button" name="save_all">{{ _('Save All Changes') }}</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Reference in new issue
Block a user