add placeholder text

This commit is contained in:
Luck
2022-01-08 19:19:16 +00:00
parent ed64391a51
commit ccbcd9182d
2 changed files with 38 additions and 3 deletions

View File

@@ -54,6 +54,7 @@ export default function Editor({
/> />
<EditorTextArea <EditorTextArea
forcedContent={forcedContent} forcedContent={forcedContent}
actualContent={actualContent}
setActualContent={setActualContent} setActualContent={setActualContent}
theme={themes[theme]} theme={themes[theme]}
language={language} language={language}

View File

@@ -6,6 +6,7 @@ import themes, { makeMonacoTheme } from '../style/themes';
export default function EditorTextArea({ export default function EditorTextArea({
forcedContent, forcedContent,
actualContent,
setActualContent, setActualContent,
theme, theme,
language, language,
@@ -16,6 +17,7 @@ export default function EditorTextArea({
const [selected, toggleSelected] = useSelectedLine(); const [selected, toggleSelected] = useSelectedLine();
const editorAreaRef = useRef(); const editorAreaRef = useRef();
useLineNumberMagic(editorAreaRef, selected, toggleSelected, editor, monaco); useLineNumberMagic(editorAreaRef, selected, toggleSelected, editor, monaco);
usePlaceholderText(actualContent, editor, monaco);
function beforeMount(monaco) { function beforeMount(monaco) {
for (const [id, theme] of Object.entries(themes)) { for (const [id, theme] of Object.entries(themes)) {
@@ -72,8 +74,40 @@ const EditorArea = styled.main`
background-color: ${props => props.theme.editor.lineNumberHlBackground}; background-color: ${props => props.theme.editor.lineNumberHlBackground};
font-weight: bold; font-weight: bold;
} }
.placeholder-text {
position: absolute;
opacity: 0.5;
font-weight: lighter;
&::after {
content: 'Paste (or type) some code...';
}
}
`; `;
function usePlaceholderText(actualContent, editor, monaco) {
useEffect(() => {
if (!editor || !monaco || actualContent) {
return;
}
const decoration = {
range: new monaco.Range(1, 1, 1, 1),
options: {
after: {
content: '\u200B',
inlineClassName: 'placeholder-text',
},
},
};
const decorations = editor.deltaDecorations([], [decoration]);
return () => {
editor.deltaDecorations(decorations, []);
};
}, [editor, monaco, actualContent]);
}
function useLineNumberMagic( function useLineNumberMagic(
editorAreaRef, editorAreaRef,
selected, selected,
@@ -105,9 +139,9 @@ function useLineNumberMagic(
return; return;
} }
const range = []; const newDecorations = [];
if (selected[0] !== -1) { if (selected[0] !== -1) {
range.push({ newDecorations.push({
range: new monaco.Range(selected[0], 1, selected[1], 1), range: new monaco.Range(selected[0], 1, selected[1], 1),
options: { options: {
isWholeLine: true, isWholeLine: true,
@@ -115,7 +149,7 @@ function useLineNumberMagic(
}, },
}); });
} }
const decorations = editor.deltaDecorations([], range); const decorations = editor.deltaDecorations([], newDecorations);
return () => { return () => {
editor.deltaDecorations(decorations, []); editor.deltaDecorations(decorations, []);