Merge branch '74-fix-sandbox-inline-sandbox-sizing-is-incorrect' into 'development'
Resolve "fix(Sandbox): Inline Sandbox sizing is incorrect" Closes #74 See merge request GeneralProtocols/xo/stack!118
This commit is contained in:
@@ -6,8 +6,9 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, onMounted, watch } from 'vue';
|
import { ref, onMounted, watch, computed } from 'vue';
|
||||||
import loader, { Monaco } from '@monaco-editor/loader';
|
import loader, { Monaco } from '@monaco-editor/loader';
|
||||||
|
import { type editor } from 'monaco-editor';
|
||||||
|
|
||||||
import { ImportedLib } from '../utils/useXOCashLibs';
|
import { ImportedLib } from '../utils/useXOCashLibs';
|
||||||
|
|
||||||
@@ -28,16 +29,26 @@ const code = defineModel<string>();
|
|||||||
|
|
||||||
const monacoInstance = ref<Monaco | null>(null);
|
const monacoInstance = ref<Monaco | null>(null);
|
||||||
const editorContainer = ref<HTMLElement | null>(null);
|
const editorContainer = ref<HTMLElement | null>(null);
|
||||||
|
const contentHeight = ref(100);
|
||||||
|
|
||||||
|
const editorHeight = computed(() => {
|
||||||
|
const codeHeight = contentHeight.value;
|
||||||
|
const height = Math.max(codeHeight, 100);
|
||||||
|
return height;
|
||||||
|
})
|
||||||
|
|
||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
(e: 'execute'): void;
|
(e: 'execute'): void;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
defineExpose({
|
||||||
|
editorHeight,
|
||||||
|
});
|
||||||
|
|
||||||
watch(props.libs, async () => {
|
watch(props.libs, async () => {
|
||||||
updateEditorTypes();
|
updateEditorTypes();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
function updateEditorTypes() {
|
function updateEditorTypes() {
|
||||||
monacoInstance.value?.languages.typescript.typescriptDefaults.setExtraLibs(
|
monacoInstance.value?.languages.typescript.typescriptDefaults.setExtraLibs(
|
||||||
Object.entries(props.libs).map(([name, content]) => {
|
Object.entries(props.libs).map(([name, content]) => {
|
||||||
@@ -128,8 +139,34 @@ onMounted(() => {
|
|||||||
editor.onDidChangeModelContent(() => {
|
editor.onDidChangeModelContent(() => {
|
||||||
code.value = editor.getValue();
|
code.value = editor.getValue();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
editor.onDidChangeModelContent(() => {
|
||||||
|
updateEditorHeight(editor);
|
||||||
|
})
|
||||||
|
|
||||||
|
updateEditorHeight(editor);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the height of the editor based on the number of lines in the editor.
|
||||||
|
* NOTE: This is a bit of a hack, as 'height: auto' does not work with Monaco Editor
|
||||||
|
*
|
||||||
|
* @param editor - The editor instance.
|
||||||
|
*/
|
||||||
|
const updateEditorHeight = (editor: editor.IStandaloneCodeEditor) => {
|
||||||
|
let lineCount = editor.getModel()?.getLineCount();
|
||||||
|
if (!lineCount) return;
|
||||||
|
|
||||||
|
// Add an extra line to the lineCount
|
||||||
|
lineCount += 1;
|
||||||
|
|
||||||
|
// get font size
|
||||||
|
const fontSize = editor.getOption(67);
|
||||||
|
|
||||||
|
const height = lineCount * fontSize
|
||||||
|
contentHeight.value = height;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="code-sandbox" :style="sandboxStyle">
|
<div class="code-sandbox" :style="sandboxStyle">
|
||||||
|
<!-- Editor Container -->
|
||||||
<div class="editor-container" :style="editorContainerStyle">
|
<div class="editor-container" :style="editorContainerStyle">
|
||||||
<code-editor v-model="code" @execute="executeCode" :style="editorStyle" :libs="libs" :is-dark="isDark"/>
|
<code-editor ref="editorRef" v-model="code" @execute="executeCode" :style="editorStyle" :libs="libs" :is-dark="isDark"/>
|
||||||
<div class="editor-buttons">
|
<div class="editor-buttons">
|
||||||
<button v-if="!hideRunButton" @click="executeCode" class="primary-button ">Run Code</button>
|
<button v-if="!hideRunButton" @click="executeCode" class="primary-button ">Run Code</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Output Container -->
|
||||||
<div v-if="showLogs || fullHeight" class="output-container" :style="outputContainerStyle">
|
<div v-if="showLogs || fullHeight" class="output-container" :style="outputContainerStyle">
|
||||||
<div class="output-container-buttons">
|
<div class="output-container-buttons">
|
||||||
<button v-if="!fullHeight" @click="showLogs = false" class="secondary-button">Close</button>
|
<button v-if="!fullHeight" @click="showLogs = false" class="secondary-button">Close</button>
|
||||||
@@ -22,6 +23,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- IFrame for running the code -->
|
||||||
<div class="iframe-container" style="display:none">
|
<div class="iframe-container" style="display:none">
|
||||||
<iframe
|
<iframe
|
||||||
ref="sandboxIframe"
|
ref="sandboxIframe"
|
||||||
@@ -93,6 +95,7 @@ const code = ref<string>('');
|
|||||||
const logs = ref<ConsoleLog[]>([]);
|
const logs = ref<ConsoleLog[]>([]);
|
||||||
const showLogs = ref(false);
|
const showLogs = ref(false);
|
||||||
const sandboxIframe = ref<HTMLIFrameElement | null>(null);
|
const sandboxIframe = ref<HTMLIFrameElement | null>(null);
|
||||||
|
const editorRef = ref<InstanceType<typeof CodeEditor> | null>(null);
|
||||||
|
|
||||||
// Computed styles based on props
|
// Computed styles based on props
|
||||||
const sandboxStyle = computed(() => ({
|
const sandboxStyle = computed(() => ({
|
||||||
@@ -101,14 +104,29 @@ const sandboxStyle = computed(() => ({
|
|||||||
flexDirection: 'column'
|
flexDirection: 'column'
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const editorContainerStyle = computed(() => ({
|
const editorContainerStyle = computed(() => {
|
||||||
height: props.fullHeight ? props.editorHeight !== 'auto' ? props.editorHeight : '50%' : 'auto',
|
const baseStyle = {
|
||||||
flex: props.fullHeight && props.editorHeight === 'auto' ? '1 0 auto' : 'none',
|
display: 'flex',
|
||||||
display: 'flex',
|
flexDirection: 'column',
|
||||||
flexDirection: 'column',
|
position: 'relative',
|
||||||
position: 'relative',
|
overflow: 'hidden', // Ensure overflowing content doesn't spill out
|
||||||
overflow: 'hidden' // Ensure overflowing content doesn't spill out
|
padding: '16px',
|
||||||
}));
|
}
|
||||||
|
|
||||||
|
if (props.fullHeight) {
|
||||||
|
return {
|
||||||
|
...baseStyle,
|
||||||
|
height: props.editorHeight !== 'auto' ? props.editorHeight : '50%',
|
||||||
|
flex: props.editorHeight === 'auto' ? '1 0 auto' : 'none',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...baseStyle,
|
||||||
|
height: `calc(${editorRef.value?.editorHeight}px + 2 * ${baseStyle.padding})`,
|
||||||
|
flex: props.editorHeight === 'auto' ? '1 0 auto' : 'none',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const editorStyle = computed(() => ({
|
const editorStyle = computed(() => ({
|
||||||
width: '100%',
|
width: '100%',
|
||||||
@@ -379,7 +397,6 @@ defineExpose({
|
|||||||
.editor-container {
|
.editor-container {
|
||||||
position: relative;
|
position: relative;
|
||||||
background: var(--vp-code-block-bg);
|
background: var(--vp-code-block-bg);
|
||||||
padding: 16px;
|
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
overflow: hidden; /* Contain overflow */
|
overflow: hidden; /* Contain overflow */
|
||||||
|
|||||||
@@ -26,10 +26,7 @@ The below is Address mess.
|
|||||||
|
|
||||||
<CodeSandbox
|
<CodeSandbox
|
||||||
:libs="xocashLibs"
|
:libs="xocashLibs"
|
||||||
height="50em"
|
>
|
||||||
editorHeight="30em"
|
|
||||||
outputHeight="20em"
|
|
||||||
fullHeight>
|
|
||||||
|
|
||||||
```
|
```
|
||||||
import { Address } from '@xocash/stack';
|
import { Address } from '@xocash/stack';
|
||||||
@@ -55,10 +52,7 @@ We can have more than one per page too. For example:
|
|||||||
|
|
||||||
<CodeSandbox
|
<CodeSandbox
|
||||||
:libs="xocashLibs"
|
:libs="xocashLibs"
|
||||||
height="50em"
|
>
|
||||||
editorHeight="30em"
|
|
||||||
outputHeight="20em"
|
|
||||||
fullHeight>
|
|
||||||
|
|
||||||
```
|
```
|
||||||
import { BlockchainElectrum } from '@xocash/stack';
|
import { BlockchainElectrum } from '@xocash/stack';
|
||||||
@@ -82,10 +76,7 @@ The below demonstrates instantiating a wallet.
|
|||||||
|
|
||||||
<CodeSandbox
|
<CodeSandbox
|
||||||
:libs="xocashLibs"
|
:libs="xocashLibs"
|
||||||
height="50em"
|
>
|
||||||
editorHeight="30em"
|
|
||||||
outputHeight="20em"
|
|
||||||
fullHeight>
|
|
||||||
|
|
||||||
```
|
```
|
||||||
import { BlockchainElectrum, WalletP2PKH } from '@xocash/stack';
|
import { BlockchainElectrum, WalletP2PKH } from '@xocash/stack';
|
||||||
|
|||||||
Reference in New Issue
Block a user