Fix shift-enter in bio editor (#6769)

* fix wrong enabled update button at first load

* fix shift-enter error

* small fixes + code clean up

---------

Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
Carina WollendorferandCarinaWolli authored and GitHub committed 2023-01-27 23:14:58 +00:00
1 parent 60f7528cbc
commit 333801d2ae
6 files changed
+47 -12

No files matched your search

@@ -12,7 +12,7 @@ import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
import { getPlaceholderAvatar } from "@calcom/lib/getPlaceholderAvatar";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import objectKeys from "@calcom/lib/objectKeys";
import turndownService from "@calcom/lib/turndownService";
import turndown from "@calcom/lib/turndownService";
import { trpc } from "@calcom/trpc/react";
import {
Avatar,
@@ -223,7 +223,7 @@ const ProfileView = () => {
<Label>{t("about")}</Label>
<Editor
getText={() => md.render(form.getValues("bio") || "")}
setText={(value: string) => form.setValue("bio", turndownService.turndown(value))}
setText={(value: string) => form.setValue("bio", turndown(value))}
excludedToolbarItems={["blockType"]}
/>
</div>
+41 -4
View File
@@ -2,11 +2,48 @@ import TurndownService from "turndown";
const turndownService = new TurndownService();
turndownService.addRule("newLine", {
filter: ["br"],
replacement: () => {
function turndown(html: string | TurndownService.Node): string {
let result = turndownService.turndown(html);
result = result.replaceAll("[<p><br></p>]", "");
if (result === "<p><br></p>") {
result = "";
}
return result;
}
turndownService.addRule("shiftEnter", {
filter: function (node) {
return node.nodeName === "BR" && !!isShiftEnter(node);
},
replacement: function () {
return "<br>";
},
});
turndownService.addRule("enter", {
filter: function (node) {
return node.nodeName === "BR" && !isShiftEnter(node);
},
replacement: function () {
return "<p><br></p>";
},
});
export default turndownService;
function isShiftEnter(node: HTMLElement) {
let currentNode: HTMLElement | null | ParentNode = node;
while (currentNode != null && currentNode.nodeType !== 1) {
currentNode = currentNode.parentElement || currentNode.parentNode;
}
return (
currentNode &&
currentNode.nodeType === 1 &&
currentNode.parentElement &&
currentNode.parentElement.childNodes.length !== 1 // normal enter is <p><br><p> (p has exactly one childNode)
);
}
export default turndown;
@@ -387,7 +387,6 @@ export default function ToolbarPlugin(props: TextEditorProps) {
editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
}
}, [editor, isLink]);
console.log("TEST", blockTypeToBlockName[blockType]);
return (
<div className="toolbar flex" ref={toolbarRef}>
<>
@@ -68,7 +68,6 @@
.editor-paragraph {
margin: 0;
margin-bottom: 8px;
position: relative;
}