diff --git a/src/components/Widgets/MarkdownControlElements/VisualEditor/index.js b/src/components/Widgets/MarkdownControlElements/VisualEditor/index.js index 1f19ea35..714f12d4 100644 --- a/src/components/Widgets/MarkdownControlElements/VisualEditor/index.js +++ b/src/components/Widgets/MarkdownControlElements/VisualEditor/index.js @@ -13,6 +13,16 @@ import { buildKeymap } from './keymap'; import Toolbar from '../Toolbar'; import styles from './index.css'; +function processUrl(url) { + if (url.match(/^(https?:\/\/|mailto:|\/)/)) { + return url; + } + if (url.match(/^[^\/]+\.[^\/]+/)) { + return `https://${ url }`; + } + return `/${ url }`; +} + function buildInputRules(schema) { let result = [], type; if (type = schema.nodes.blockquote) result.push(blockQuoteRule(type)); @@ -23,6 +33,14 @@ function buildInputRules(schema) { return result; } +function markActive(state, type) { + const { from, to, empty } = state.selection; + if (empty) { + return type.isInSet(state.storedMarks || state.doc.marksAt(from)); + } + return state.doc.rangeHasMark(from, to, type); +} + export default class Editor extends Component { constructor(props) { super(props); @@ -110,6 +128,15 @@ export default class Editor extends Component { command(this.view.state, this.handleAction); }; + handleLink = () => { + let url = null; + if (!markActive(this.view.state, schema.marks.link)) { + url = prompt('Link URL:'); + } + const command = toggleMark(schema.marks.link, { href: url ? processUrl(url) : null }); + command(this.view.state, this.handleAction); + }; + handleToggle = () => { this.props.onMode('raw'); };