From ccbcd9182d88deb87844b7768e92969adce2446a Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 8 Jan 2022 19:19:16 +0000 Subject: [PATCH] add placeholder text --- src/components/Editor.js | 1 + src/components/EditorTextArea.js | 40 +++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/components/Editor.js b/src/components/Editor.js index 01c85de..8367f68 100644 --- a/src/components/Editor.js +++ b/src/components/Editor.js @@ -54,6 +54,7 @@ export default function Editor({ /> props.theme.editor.lineNumberHlBackground}; 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( editorAreaRef, selected, @@ -105,9 +139,9 @@ function useLineNumberMagic( return; } - const range = []; + const newDecorations = []; if (selected[0] !== -1) { - range.push({ + newDecorations.push({ range: new monaco.Range(selected[0], 1, selected[1], 1), options: { isWholeLine: true, @@ -115,7 +149,7 @@ function useLineNumberMagic( }, }); } - const decorations = editor.deltaDecorations([], range); + const decorations = editor.deltaDecorations([], newDecorations); return () => { editor.deltaDecorations(decorations, []);