From fc89d04acc3fdb9af85097b02681f5a788a6c3cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E5=86=89?= Date: Sun, 27 Oct 2019 18:16:29 +0800 Subject: [PATCH] feat: add update to footnote --- src/muya/lib/assets/pngicon/footnote/2.png | Bin 0 -> 1675 bytes src/muya/lib/assets/styles/index.css | 50 ++++++++++++++++++ src/muya/lib/contentState/footnoteCtrl.js | 43 +++++++++++++++ src/muya/lib/contentState/index.js | 2 + src/muya/lib/contentState/updateCtrl.js | 9 +++- .../parser/render/renderBlock/renderIcon.js | 4 +- src/muya/lib/parser/rules.js | 2 +- src/renderer/assets/styles/index.css | 1 + src/renderer/assets/themes/dark.theme.css | 1 + src/renderer/assets/themes/graphite.theme.css | 1 + .../assets/themes/material-dark.theme.css | 1 + src/renderer/assets/themes/one-dark.theme.css | 1 + src/renderer/assets/themes/ulysses.theme.css | 1 + 13 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 src/muya/lib/assets/pngicon/footnote/2.png create mode 100644 src/muya/lib/contentState/footnoteCtrl.js diff --git a/src/muya/lib/assets/pngicon/footnote/2.png b/src/muya/lib/assets/pngicon/footnote/2.png new file mode 100644 index 0000000000000000000000000000000000000000..f5be4b5e3f9c7cfada36dc825a84256e161fe2e3 GIT binary patch literal 1675 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST~P>fi1|>#WAEJ?(N-+d6xqOTrYn3 zFVDN`h_u3;oql_-M%q+gGX7ohe3z?N-JZDh^Vg^U`&;qsc-`#UuiseBFC6#f`Xb&? zw>PeQzv7Fx+*Qga#2xA-Vtb<^G1UrkHofIX^9nk6Lf$Fk$tQe|2~7sz1DH z(*Na|;Oi{g=hyGnCfgPr-t$mpkJq<}wVXv;=Y4(+v~Gez`X4=id!}E!@AuqK?O%P= zcGG&{Cm;=$j^`v`mRmdOeSf?@;$K&3&E}dt2g(KiYW28(``S?oGQ}oot|81B-#SWn z%J)aeZS2bz>yOy3@aKPb#gSk3i+8X4&{694Y2xJh3h7I0(`G5mU-M~^O{mfD}$q!?}>>j4ii#zokBST zmJAW8eQZ{d-2v<@dH;VkN5_BoT4gzv@0Mwm&x32fZ*mshX&3JRhRhm)!YCGwORt<~ zNia4Y;SyZ#+Mux3Zb|20mU>X;81>q-*`cGsKZ$>;qE|nIW7S*_5haHS0g{Wp03+yn zr;8#J3+K>~I?%4Ly)LFE-?dX0R>ymUJzqqxp`@etvk>Mhte{*8Jlu4Z_QI?Vv$TV-U7^A+e~uL3o>R= z)SwVOYuod|YuyQMqg$s0m|U6O|CsmX(L5tXgMsz_4_3uT5 { + ContentState.prototype.updateFootnote = function (block, line) { + const { start, end } = this.cursor + const { text } = line + const match = FOOTNOTE_REG.exec(text) + const footnoteIdentifer = match[1] + const sectionWrapper = this.createBlock('figure', { + functionType: 'footnote' + }) + const footnoteInput = this.createBlock('span', { + text: footnoteIdentifer, + functionType: 'footnoteInput' + }) + const pBlock = this.createBlockP(text.substring(match[0].length)) + this.appendChild(sectionWrapper, footnoteInput) + this.appendChild(sectionWrapper, pBlock) + this.insertBefore(sectionWrapper, block) + this.removeBlock(block) + + const { key } = pBlock.children[0] + this.cursor = { + start: { + key, + offset: Math.max(0, start.offset - footnoteIdentifer.length) + }, + end: { + key, + offset: Math.max(0, end.offset - footnoteIdentifer.length) + } + } + + if (this.isCollapse()) { + this.checkInlineUpdate(pBlock.children[0]) + } + + return this.partialRender() + } +} + +export default footnoteCtrl diff --git a/src/muya/lib/contentState/index.js b/src/muya/lib/contentState/index.js index 47ce4214..735dd93b 100644 --- a/src/muya/lib/contentState/index.js +++ b/src/muya/lib/contentState/index.js @@ -28,6 +28,7 @@ import emojiCtrl from './emojiCtrl' import imageCtrl from './imageCtrl' import linkCtrl from './linkCtrl' import dragDropCtrl from './dragDropCtrl' +import footnoteCtrl from './footnoteCtrl' import importMarkdown from '../utils/importMarkdown' import Cursor from '../selection/cursor' import escapeCharactersMap, { escapeCharacters } from '../parser/escapeCharacter' @@ -58,6 +59,7 @@ const prototypes = [ imageCtrl, linkCtrl, dragDropCtrl, + footnoteCtrl, importMarkdown ] diff --git a/src/muya/lib/contentState/updateCtrl.js b/src/muya/lib/contentState/updateCtrl.js index fadccfab..baedb065 100644 --- a/src/muya/lib/contentState/updateCtrl.js +++ b/src/muya/lib/contentState/updateCtrl.js @@ -10,6 +10,7 @@ const INLINE_UPDATE_FRAGMENTS = [ '^(?:[\\s\\S]+?)\\n {0,3}(\\={3,}|\\-{3,})(?= {1,}|$)', // Setext headings **match from beginning** '(?:^|\n) {0,3}(>).+', // Block quote '^( {4,})', // Indent code **match from beginning** + '^(\\[\\^[^\\^\\[\\]\\s]+?(? { if (/figure/.test(block.type)) { return false } - if (/cellContent|codeContent|languageInput/.test(block.functionType)) { + if (/cellContent|codeContent|languageInput|footnoteInput/.test(block.functionType)) { return false } @@ -89,8 +90,9 @@ const updateCtrl = ContentState => { const listItem = this.getParent(block) const [ match, bullet, tasklist, order, atxHeader, - setextHeader, blockquote, indentCode, hr + setextHeader, blockquote, indentCode, footnote, hr ] = text.match(INLINE_UPDATE_REG) || [] + const { footnote: isSupportFootnote } = this.muya.options switch (true) { case (!!hr && new Set(hr.split('').filter(i => /\S/.test(i))).size === 1): @@ -118,6 +120,9 @@ const updateCtrl = ContentState => { case !!indentCode: return this.updateIndentCode(block, line) + case !!footnote && block.type === 'p' && !block.parent && isSupportFootnote: + return this.updateFootnote(block, line) + case !match: default: return this.updateToParagraph(block, line) diff --git a/src/muya/lib/parser/render/renderBlock/renderIcon.js b/src/muya/lib/parser/render/renderBlock/renderIcon.js index 761e1e43..bd9ae3e0 100644 --- a/src/muya/lib/parser/render/renderBlock/renderIcon.js +++ b/src/muya/lib/parser/render/renderBlock/renderIcon.js @@ -21,6 +21,7 @@ import flowchartIcon from '../../../assets/pngicon/flowchart/2.png' import sequenceIcon from '../../../assets/pngicon/sequence/2.png' import mermaidIcon from '../../../assets/pngicon/mermaid/2.png' import vegaIcon from '../../../assets/pngicon/chart/2.png' +import footnoteIcon from '../../../assets/pngicon/footnote/2.png' const FUNCTION_TYPE_HASH = { mermaid: mermaidIcon, @@ -32,7 +33,8 @@ const FUNCTION_TYPE_HASH = { multiplemath: mathblockIcon, fencecode: codeIcon, indentcode: codeIcon, - frontmatter: frontMatterIcon + frontmatter: frontMatterIcon, + footnote: footnoteIcon } export default function renderIcon (block) { diff --git a/src/muya/lib/parser/rules.js b/src/muya/lib/parser/rules.js index ff2deb49..070a817f 100644 --- a/src/muya/lib/parser/rules.js +++ b/src/muya/lib/parser/rules.js @@ -42,6 +42,6 @@ export const inlineExtensionRules = { // This is not the best regexp, because it not support `2^2\\^`. superscript: /^(\^)((?:[^\^\s]|(?<=\\)\1|(?<=\\) )+?)(?