Fixed Safari regex lookahead / lookbehind issue.

This commit is contained in:
v 2021-03-03 20:29:23 +01:00
parent 65caaca659
commit 19beabe2d1
4 changed files with 10751 additions and 28 deletions

8901
prose/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,8 @@
"devDependencies": { "devDependencies": {
"@babel/core": "^7.8.7", "@babel/core": "^7.8.7",
"@babel/preset-env": "^7.9.0", "@babel/preset-env": "^7.9.0",
"babel-loader": "^8.0.6" "babel-loader": "^8.0.6",
"prettier": "^2.2.1"
}, },
"scripts": { "scripts": {
"develop": "webpack --mode development --watch", "develop": "webpack --mode development --watch",

View File

@ -23,7 +23,7 @@ let $title = document.querySelector("#title");
let $content = document.querySelector("#content"); let $content = document.querySelector("#content");
// Bugs: // Bugs:
// 1. When there's just an empty line and a hard break is inserted with shift-enter then two enters are inserted // 1. When there's just an empty line and a hard break is inserted with shift-enter then two enters are inserted
// which do not show up in the markdown ( maybe bc. they are training enters ) // which do not show up in the markdown ( maybe bc. they are training enters )
class ProseMirrorView { class ProseMirrorView {
@ -42,7 +42,10 @@ class ProseMirrorView {
const doc = writeAsMarkdownParser.parse( const doc = writeAsMarkdownParser.parse(
// Replace all "solo" \n's with \\\n for correct markdown parsing // Replace all "solo" \n's with \\\n for correct markdown parsing
content.replaceAll(/(?<!\n)\n(?!\n)/g, "\\\n") // Can't use lookahead or lookbehind because it's not supported on Safari
content.replaceAll(/([^]{0,1})(\n)([^]{0,1})/g, (match, p1, p2, p3) => {
return p1 !== "\n" && p3 !== "\n" ? p1 + "\\\n" + p3 : match;
})
); );
this.view = new EditorView(target, { this.view = new EditorView(target, {
@ -55,8 +58,10 @@ class ProseMirrorView {
return true; return true;
}, },
"Mod-k": () => { "Mod-k": () => {
const linkButton = document.querySelector(".ProseMirror-icon[title='Add or remove link']") const linkButton = document.querySelector(
linkButton.dispatchEvent(new Event('mousedown')); ".ProseMirror-icon[title='Add or remove link']"
);
linkButton.dispatchEvent(new Event("mousedown"));
return true; return true;
}, },
}), }),
@ -71,7 +76,9 @@ class ProseMirrorView {
const newContent = writeAsMarkdownSerializer const newContent = writeAsMarkdownSerializer
.serialize(newState.doc) .serialize(newState.doc)
// Replace all \\\ns ( not followed by a \n ) with \n // Replace all \\\ns ( not followed by a \n ) with \n
.replaceAll(/\\\n(?!\n)/g, "\n"); .replaceAll(/(\\\n)(\n{0,1})/g, (match, p1, p2) =>
p2 !== "\n" ? "\n" + p2 : match
);
$content.value = newContent; $content.value = newContent;
let draft = ""; let draft = "";
if ($title.value != null && $title.value !== "") { if ($title.value != null && $title.value !== "") {
@ -79,7 +86,7 @@ class ProseMirrorView {
} }
draft += newContent; draft += newContent;
clearTimeout(typingTimer); clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval); typingTimer = setTimeout(doneTyping, doneTypingInterval);
this.updateState(newState); this.updateState(newState);
}, },
}); });

File diff suppressed because one or more lines are too long