Skip to content

Commit

Permalink
Open url on cttrl click press on edit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubh1692 committed Jun 6, 2021
1 parent 3cee225 commit 61011b4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -265,6 +265,7 @@ Thanks goes to these wonderful people:

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

## Acknowledgements
Expand Down
40 changes: 39 additions & 1 deletion src/client/containers/NoteEditor.tsx
Expand Up @@ -11,12 +11,12 @@ import { EmptyEditor } from '@/components/Editor/EmptyEditor'
import { PreviewEditor } from '@/components/Editor/PreviewEditor'
import { getSync, getSettings, getNotes } from '@/selectors'
import { setPendingSync } from '@/slices/sync'

import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/base16-light.css'
import 'codemirror/mode/gfm/gfm'
import 'codemirror/addon/selection/active-line'
import 'codemirror/addon/scroll/scrollpastend'
import { urlElementConstants } from '@/utils/constants'

export const NoteEditor: React.FC = () => {
// ===========================================================================
Expand All @@ -38,6 +38,42 @@ export const NoteEditor: React.FC = () => {
const _updateNote = (note: NoteItem) => {
!pendingSync && dispatch(setPendingSync())
dispatch(updateNote(note))
addUrlElementListener()
}

const addUrlElementListener = () => {
const urlElements: any = document.getElementsByClassName(urlElementConstants.className) || []
for (const urlElement of urlElements) {
urlElement.addEventListener('mouseover', (e: any) => {
urlElement.style.textDecoration = 'underline'
urlElement.title = urlElementConstants.tooltip
if (e.ctrlKey) {
urlElement.style.cursor = 'pointer'
} else {
urlElement.style.cursor = 'inherit'
}
})
urlElement.addEventListener('mouseleave', () => {
urlElement.style.textDecoration = 'none'
urlElement.style.cursor = 'inherit'
})
urlElement.addEventListener('mousedown', (e: any) => {
if (!e.ctrlKey) {
return
}
const [url] = e.target.innerText.match(urlElementConstants.urlRegex) || []
if (url) window.open(url.replace(')', ''), '_blank')
})
}
}

const removeUrlElementListener = () => {
const urlElements: any = document.getElementsByClassName(urlElementConstants.className) || []
for (const urlElement of urlElements) {
urlElement.removeEventListener('mouseover')
urlElement.removeEventListener('mouseleave')
urlElement.removeEventListener('click')
}
}

const renderEditor = () => {
Expand All @@ -61,11 +97,13 @@ export const NoteEditor: React.FC = () => {
className="editor mousetrap"
value={activeNote.text}
options={codeMirrorOptions}
editorWillUnmount={removeUrlElementListener}
editorDidMount={(editor) => {
setTimeout(() => {
editor.focus()
}, 0)
editor.setCursor(0)
addUrlElementListener()
}}
onBeforeChange={(editor, data, value) => {
_updateNote({
Expand Down
6 changes: 6 additions & 0 deletions src/client/utils/constants.ts
Expand Up @@ -32,3 +32,9 @@ export const directionTextOptions = [
{ value: DirectionText.LEFT_TO_RIGHT, label: 'Left to right' },
{ value: DirectionText.RIGHT_TO_LEFT, label: 'Right to left' },
]

export const urlElementConstants = {
className: 'cm-url',
tooltip: 'Follow link (ctrl + click)',
urlRegex: /\bhttps?:\/\/\S+/gi,
}

0 comments on commit 61011b4

Please sign in to comment.