1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/ReactNativeClient/MarkdownEditor/applyWebLinkFormat.js
2020-05-05 19:03:13 +01:00

39 lines
1.3 KiB
JavaScript

import { isStringWebLink, replaceBetween } from './utils';
export const writeUrlTextHere = 'https://example.com';
export const writeTextHereString = 'Write some text here';
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
export default ({ getState, item, setState }) => {
const { selection, text } = getState();
let newText;
let newSelection;
const selectedText = text.substring(selection.start, selection.end);
if (selection.start !== selection.end) {
if (isStringWebLink(selectedText)) {
newText = replaceBetween(text, selection, `[${writeTextHereString}](${selectedText})`);
newSelection = {
start: selection.start + 1,
end: selection.start + 1 + writeTextHereString.length,
};
} else {
newText = replaceBetween(text, selection, `[${selectedText}](${writeUrlTextHere})`);
newSelection = {
start: selection.end + 3,
end: selection.end + 3 + writeUrlTextHere.length,
};
}
} else {
newText = replaceBetween(text, selection, `[${writeTextHereString}](${writeUrlTextHere})`);
newSelection = {
start: selection.start + 1,
end: selection.start + 1 + writeTextHereString.length,
};
}
setState({ text: newText }, () => {
setTimeout(() => {
setState({ selection: newSelection });
}, 25);
});
};