Link support in prose mirror
This commit is contained in:
@ -13,6 +13,16 @@ import { buildKeymap } from './keymap';
|
|||||||
import Toolbar from '../Toolbar';
|
import Toolbar from '../Toolbar';
|
||||||
import styles from './index.css';
|
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) {
|
function buildInputRules(schema) {
|
||||||
let result = [], type;
|
let result = [], type;
|
||||||
if (type = schema.nodes.blockquote) result.push(blockQuoteRule(type));
|
if (type = schema.nodes.blockquote) result.push(blockQuoteRule(type));
|
||||||
@ -23,6 +33,14 @@ function buildInputRules(schema) {
|
|||||||
return result;
|
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 {
|
export default class Editor extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
@ -110,6 +128,15 @@ export default class Editor extends Component {
|
|||||||
command(this.view.state, this.handleAction);
|
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 = () => {
|
handleToggle = () => {
|
||||||
this.props.onMode('raw');
|
this.props.onMode('raw');
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user