Skip to content

Commit

Permalink
#438 inserted addOverlay function
Browse files Browse the repository at this point in the history
  • Loading branch information
pmpwith2i committed Jun 1, 2021
1 parent 3cee225 commit ff83dd6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/client/containers/NoteEditor.tsx
Expand Up @@ -2,14 +2,15 @@ import dayjs from 'dayjs'
import React from 'react'
import { Controlled as CodeMirror } from 'react-codemirror2'
import { useDispatch, useSelector } from 'react-redux'
import { Editor } from 'codemirror'

import { getActiveNote } from '@/utils/helpers'
import { updateNote } from '@/slices/note'
import { NoteItem } from '@/types'
import { NoteMenuBar } from '@/containers/NoteMenuBar'
import { EmptyEditor } from '@/components/Editor/EmptyEditor'
import { PreviewEditor } from '@/components/Editor/PreviewEditor'
import { getSync, getSettings, getNotes } from '@/selectors'
import { getNotes, getSettings, getSync } from '@/selectors'
import { setPendingSync } from '@/slices/sync'

import 'codemirror/lib/codemirror.css'
Expand Down Expand Up @@ -40,6 +41,25 @@ export const NoteEditor: React.FC = () => {
dispatch(updateNote(note))
}

const setEditorOverlay = (editor: Editor) => {
const query = /\{\{[^}]*}}/g
editor.addOverlay({
token: function (stream: any) {
query.lastIndex = stream.pos
var match = query.exec(stream.string)
if (match && match.index == stream.pos) {
stream.pos += match[0].length || 1

return 'notelink'
} else if (match) {
stream.pos = match.index
} else {
stream.skipToEnd()
}
},
})
}

const renderEditor = () => {
if (loading) {
return <div className="empty-editor v-center">Loading...</div>
Expand All @@ -66,6 +86,7 @@ export const NoteEditor: React.FC = () => {
editor.focus()
}, 0)
editor.setCursor(0)
setEditorOverlay(editor)
}}
onBeforeChange={(editor, data, value) => {
_updateNote({
Expand Down
6 changes: 6 additions & 0 deletions src/client/styles/_editor.scss
Expand Up @@ -32,3 +32,9 @@
.CodeMirror-activeline-background {
background: rgba(0, 0, 0, 0.05) !important;
}

.cm-notelink {
font-style: italic;
font-weight: bold;
color: #0daba3;
}

0 comments on commit ff83dd6

Please sign in to comment.