Skip to content

Commit

Permalink
🐛 #588 粘贴会换行
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Jul 17, 2020
1 parent 0443442 commit 1a25216
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
26 changes: 25 additions & 1 deletion src/ts/sv/process.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import {getMarkdown} from "../markdown/getMarkdown";
import {accessLocalStorage} from "../util/compatibility";
import {hasClosestBlock} from "../util/hasClosest";
import {scrollCenter} from "../util/editorCommonEvent";
import {hasClosestBlock, hasClosestByAttribute} from "../util/hasClosest";
import {hasClosestByTag} from "../util/hasClosestByHeadings";
import {log} from "../util/log";
import {getEditorRange, setRangeByWbr} from "../util/selection";
import {inputEvent} from "./inputEvent";

export const processPaste = (vditor: IVditor, text: string) => {
const range = getEditorRange(vditor.sv.element);
range.extractContents();
range.insertNode(document.createTextNode(Lute.Caret));
range.insertNode(document.createTextNode(text));
let blockElement = hasClosestByAttribute(range.startContainer, "data-block", "0");
if (!blockElement) {
blockElement = vditor.sv.element;
}
const html = "<div data-block='0'>" +
vditor.lute.Md2VditorSVDOM(blockElement.textContent).replace(/<span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span><span data-type="newline"><br \/><span style="display: none">\n<\/span><\/span></g, '<span data-type="newline"><br /><span style="display: none">\n</span></span><span data-type="newline"><br /><span style="display: none">\n</span></span></div><div data-block="0"><') +
"</div>";
if (blockElement.isEqualNode(vditor.sv.element)) {
blockElement.innerHTML = html;
} else {
blockElement.outerHTML = html;
}
setRangeByWbr(vditor.sv.element, range);

scrollCenter(vditor);
};

const getPreviousNL = (spanElement: Element) => {
let previousElement = spanElement;
while (previousElement && previousElement.getAttribute("data-type") !== "newline") {
Expand All @@ -16,6 +39,7 @@ const getPreviousNL = (spanElement: Element) => {
}
return false;
};

export const processSpinVditorSVDOM = (html: string, vditor: IVditor) => {
log("SpinVditorSVDOM", html, "argument", vditor.options.debugger);
html = "<div data-block='0'>" +
Expand Down
9 changes: 4 additions & 5 deletions src/ts/util/fixBrowserBehavior.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Constants} from "../constants";
import {input as IRInput} from "../ir/input";
import {processAfterRender} from "../ir/process";
import {processAfterRender as processSVAfterRender} from "../sv/process";
import {processAfterRender as processSVAfterRender, processPaste} from "../sv/process";
import {uploadFiles} from "../upload";
import {setHeaders} from "../upload/setHeaders";
import {processCodeRender, processPasteCode} from "../util/processCode";
Expand Down Expand Up @@ -1193,7 +1193,6 @@ export const paste = (vditor: IVditor, event: ClipboardEvent & { target: HTMLEle
const renderers: {
HTML2VditorDOM?: ILuteRender,
HTML2VditorIRDOM?: ILuteRender,
HTML2VditorSVDOM?: ILuteRender,
Md2VditorDOM?: ILuteRender,
Md2VditorIRDOM?: ILuteRender,
Md2VditorSVDOM?: ILuteRender,
Expand Down Expand Up @@ -1307,9 +1306,9 @@ export const paste = (vditor: IVditor, event: ClipboardEvent & { target: HTMLEle
vditor.lute.SetJSRenderers({renderers});
insertHTML(vditor.lute.HTML2VditorDOM(tempElement.innerHTML), vditor);
} else {
renderers.HTML2VditorSVDOM = {renderLinkDest};
renderers.Md2VditorSVDOM = {renderLinkDest};
vditor.lute.SetJSRenderers({renderers});
insertHTML(vditor.lute.HTML2VditorSVDOM(tempElement.innerHTML), vditor);
processPaste(vditor, vditor.lute.HTML2Md(tempElement.innerHTML).trimRight());
}
vditor.outline.render(vditor);
} else if (event.clipboardData.files.length > 0 && vditor.options.upload.url) {
Expand All @@ -1326,7 +1325,7 @@ export const paste = (vditor: IVditor, event: ClipboardEvent & { target: HTMLEle
} else {
renderers.Md2VditorSVDOM = {renderLinkDest};
vditor.lute.SetJSRenderers({renderers});
insertHTML(vditor.lute.Md2VditorSVDOM(textPlain), vditor);
processPaste(vditor, textPlain);
}
vditor.outline.render(vditor);
}
Expand Down
10 changes: 1 addition & 9 deletions src/ts/util/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,9 @@ export const insertHTML = (html: string, vditor: IVditor) => {
// 使用 lute 方法会添加 p 元素,只有一个 p 元素的时候进行删除
const tempElement = document.createElement("div");
tempElement.innerHTML = html;
const tempBlockElement = vditor.currentMode === "sv" ?
tempElement.querySelectorAll('[data-type="p"]') :
tempElement.querySelectorAll("p");
const tempBlockElement = tempElement.querySelectorAll("p");
if (tempBlockElement.length === 1 && !tempBlockElement[0].previousSibling && !tempBlockElement[0].nextSibling &&
vditor[vditor.currentMode].element.children.length > 0) {
// empty and past
if (vditor.currentMode === "sv") {
tempBlockElement[0].querySelectorAll('[data-type="newline"]').forEach((item: HTMLElement) => {
item.remove();
});
}
html = tempBlockElement[0].innerHTML.trim();
}

Expand Down
8 changes: 2 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ interface ILute {
renderers: {
HTML2VditorDOM?: ILuteRender,
HTML2VditorIRDOM?: ILuteRender,
HTML2VditorSVDOM?: ILuteRender,
HTML2Md?: ILuteRender,
Md2HTML?: ILuteRender,
Md2VditorDOM?: ILuteRender,
Expand Down Expand Up @@ -205,10 +204,7 @@ interface ILute {
// sv 输入渲染
SpinVditorSVDOM(text: string): string;

// 粘贴时将 html 转换为 sv
HTML2VditorSVDOM(html: string): string;

// md 转换为 sv
// 粘贴是 md 转换为 sv
Md2VditorSVDOM(text: string): string;
}

Expand Down Expand Up @@ -344,7 +340,7 @@ interface IMarkdownConfig {
footnotes?: boolean;
/** wysiwyg & ir 模式代码块是否渲染。默认值: true */
codeBlockPreview: boolean;
/** 是否解析 setext 标题。默认值: false */
/** 是否解析 setext 标题。默认值: true */
setext: boolean;
/** 是否启用过滤 XSS。默认值: true */
sanitize: boolean;
Expand Down

0 comments on commit 1a25216

Please sign in to comment.