Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
* dev:
  feat: #379 通过引入第三方组件cm-search-replace实现左侧编辑器搜索、替换功能
  fix: #380 修复粘贴时转义&的问题,同时修复粘贴table时出现过多换行的问题\n feat: 当表格thead为空的时候,不再渲染<thead>
  fix: #374 光标处于编辑区域最低部时,预览区域滚动到了顶部
  • Loading branch information
jiawei686 committed Jan 13, 2023
2 parents c5f983e + 3b7d17d commit ef0d765
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 49 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"dependencies": {
"@types/codemirror": "^0.0.108",
"@types/dompurify": "^2.2.3",
"cm-search-replace": "^1.0.0",
"jsdom": "~19.0.0",
"mitt": "^3.0.0"
},
Expand Down
4 changes: 4 additions & 0 deletions src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import 'codemirror/addon/edit/matchtags';
import 'codemirror/addon/search/searchcursor';
import 'codemirror/addon/display/placeholder';
import 'codemirror/keymap/sublime';

import 'cm-search-replace/src/search';
import 'codemirror/addon/scroll/annotatescrollbar';
import 'codemirror/addon/search/matchesonscrollbar';
// import 'codemirror/addon/selection/active-line';
// import 'codemirror/addon/edit/matchbrackets';
import htmlParser from '@/utils/htmlparser';
Expand Down
2 changes: 2 additions & 0 deletions src/Previewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,8 @@ export default class Previewer {
return scrollTo;
}
}
// 如果计算完预览区域所有的行号依然<左侧光标所在的行号,则预览区域直接滚到最低部
return domContainer.scrollHeight;
}

/**
Expand Down
17 changes: 16 additions & 1 deletion src/core/hooks/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,23 @@ export default class Table extends ParagraphBase {
};
}

/**
* 如果table.head是空的,就不渲染<thead>了
* @param {String} str
* @returns {Boolean}
*/
$testHeadEmpty(str) {
const test = str
.replace(/&nbsp;/g, '')
.replace(/\s/g, '')
.replace(/(~CTH\$|~CTHU|~CTHL|~CTHR|~CTHC)/g, '');
return test?.length > 0;
}

$renderTable(COLUMN_ALIGN_MAP, tableHeader, tableRows, dataLines) {
const cacheSrc = `~CTHD${tableHeader}~CTHD$~CTBD${tableRows}~CTBD$`;
const cacheSrc = this.$testHeadEmpty(tableHeader)
? `~CTHD${tableHeader}~CTHD$~CTBD${tableRows}~CTBD$`
: `~CTBD${tableRows}~CTBD$`;
const html = cacheSrc;
const sign = this.$engine.md5(html);
const renderHtml = html
Expand Down
7 changes: 7 additions & 0 deletions src/sass/cherry.scss
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@
}
}


.cherry {
.ace_search {
background: #FFF;
}
}

.cherry-sidebar {
width: 30px;
position: absolute;
Expand Down
60 changes: 12 additions & 48 deletions src/utils/htmlparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const htmlParser = {
.replace(/\n{3,}/g, '\n\n\n')
.replace(/&gt;/g, '>')
.replace(/&lt;/g, '<')
.replace(/&amp;/g, '&')
.trim('\n');
},
/**
Expand All @@ -68,43 +69,7 @@ const htmlParser = {
*/
$handleTagObject(temObj, returnString) {
let ret = returnString;
if (temObj.attrs.class && temObj.attrs.class.indexOf('mermaid') >= 0) {
// 针对 mermaid 图
try {
ret += [
'\n```',
temObj.attrs['data-type'],
'\n',
decodeURIComponent(atob(temObj.attrs['data-code'])),
'\n```\n',
].join('');
} catch (e) {
ret += [
'\n```',
temObj.attrs['data-type'],
'\n',
decodeURIComponent(temObj.attrs['data-code']),
'\n```\n',
].join('');
}
} else if (temObj.attrs.class && temObj.attrs.class.indexOf('mathjax-wrapper') >= 0) {
// 针对公式,首尾加空格是为了适应新语法
try {
ret += ` ${decodeURIComponent(atob(temObj.attrs['data-source']))} `;
} catch (e) {
ret += ` ${decodeURIComponent(temObj.attrs['data-source'])} `;
}
} else if (temObj.attrs['data-control'] && temObj.attrs['data-control'] === 'tapd-table') {
// 处理高阶表格 cherryMarkdown里不需要这段逻辑
ret += ['\n```', ' tapd-table ', temObj.attrs['data-size'], '\n'].join('');

if (temObj.children[1] && temObj.children[1].children[0].content) {
const data = temObj.children[1].children[0].content.replace(/\s+/g, '');
ret += ['\n', data, '\n```\n'].join('');
} else {
ret += ['\n', '"工作表":{"数据":{"19::25":" "}} ', '\n```\n'].join(''); // 高阶数据存在问题,转换为默认
}
} else if (temObj.attrs.class && temObj.attrs.class.indexOf('ch-icon') >= 0) {
if (temObj.attrs.class && temObj.attrs.class.indexOf('ch-icon') >= 0) {
// 针对checklist
if (temObj.attrs.class.indexOf('ch-icon-check') >= 0) {
ret += '[x]';
Expand Down Expand Up @@ -168,8 +133,7 @@ const htmlParser = {
// 递归找到对应的代码文本
ret += self.$dealCodeTag(temObj);
} else {
// 代码块会对<、>做转义,转成markdown时就不需要转义了
ret += temObj.content.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
ret += temObj.content;
}
}
return ret;
Expand Down Expand Up @@ -761,7 +725,7 @@ const htmlParser = {
},
convertCode(str) {
if (/\n/.test(str)) {
return `\n\`\`\`\n${str.replace(/\n+$/, '')}\n\`\`\`\n`;
return `\`\`\`\n${str.replace(/\n+$/, '')}\n\`\`\``;
}
return ` \`${str.replace(/`/g, '\\`')}\` `;
},
Expand Down Expand Up @@ -818,19 +782,19 @@ const htmlParser = {
return `^^${str.trim().replace(/\^\^/g, '\\^\\^')}^^`;
},
convertTd(str) {
return `~|${str.trim().replace(/\n/g, '<br>')} ~|`;
return `~|${str.trim().replace(/\n{1,}/g, '<br>')} ~|`;
},
convertTh(str) {
return `~|${str.trim().replace(/\n/g, '<br>')} ~|`;
return `~|${str.trim().replace(/\n{1,}/g, '<br>')} ~|`;
},
convertTr(str) {
return `${str}\n`;
return `${str.replace(/\n/g, '')}\n`;
},
convertThead(str) {
return `${str.replace(/~\|~\|/g, '~|').replace(/~\|/g, '|')}|:--|\n`;
},
convertTable(str) {
const ret = `\n${str.replace(/~\|~\|/g, '~|').replace(/~\|/g, '|')}\n`;
const ret = `\n${str.replace(/~\|~\|/g, '~|').replace(/~\|/g, '|')}\n`.replace(/\n{2,}/g, '\n');
if (/\|:--\|/.test(ret)) {
return ret;
}
Expand All @@ -840,7 +804,7 @@ const htmlParser = {
return `- ${str.replace(/^\n/, '').replace(/\n+$/, '').replace(/\n+/g, '\n\t')}\n`;
},
convertUl(str) {
return `\n\n${str}\n\n`;
return `${str}\n`;
},
convertOl(str) {
const arr = str.split('\n');
Expand All @@ -852,7 +816,7 @@ const htmlParser = {
}
}
const $str = arr.join('\n');
return `\n\n${$str}\n\n`;
return `${$str}\n`;
},
convertStrong(str) {
return /^\s*$/.test(str) ? '' : `**${str}**`;
Expand Down Expand Up @@ -885,10 +849,10 @@ const htmlParser = {
return `###### ${str.trim().replace(/\n+$/, '')}\n\n`;
},
convertBlockquote(str) {
return `\n>${str.trim()}\n\n`;
return `>${str.trim()}\n\n`;
},
convertAddress(str) {
return `\n>${str.trim()}\n\n`;
return `>${str.trim()}\n\n`;
},
},
/**
Expand Down

0 comments on commit ef0d765

Please sign in to comment.