Link support in prose mirror

This commit is contained in:
Mathias Biilmann Christensen 2016-11-01 17:58:19 -07:00
parent 27d33d2a84
commit f02bd9a789

View File

@ -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');
};