1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/ReactNativeClient/MarkdownEditor/applyWrapFormatNewLines.js

57 lines
1.1 KiB
JavaScript

import { replaceBetween } from './utils';
const shim = require('lib/shim').default;
export default ({ getState, item, setState }) => {
const { text, selection } = getState();
let newText = replaceBetween(
text,
selection,
`\n${item.wrapper.concat(
'\n',
text.substring(selection.start, selection.end),
'\n',
item.wrapper,
'\n'
)}`
);
let newPosition;
if (selection.start === selection.end) {
newPosition = selection.end + item.wrapper.length + 2; // +2 For two new lines
newText = replaceBetween(
text,
selection,
`\n${item.wrapper.concat(
'\n',
text.substring(selection.start, selection.end),
'\n',
item.wrapper,
'\n'
)}`
);
} else {
newPosition = selection.end + item.wrapper.length * 2 + 3; // +3 For three new lines
newText = replaceBetween(
text,
selection,
`${item.wrapper.concat(
'\n',
text.substring(selection.start, selection.end),
'\n',
item.wrapper,
'\n'
)}`
);
}
const extra = {
selection: {
start: newPosition,
end: newPosition,
},
};
setState({ text: newText }, () => {
shim.setTimeout(() => {
setState({ ...extra });
}, 25);
});
};