diff --git a/package.json b/package.json index 304d5842..418c7cd4 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "eslint-plugin-vue": "^7.17.0", "husky": "^7.0.2", "prettier": "^2.3.2", - "vditor": "^3.8.6", "vue": "^3.2.6", "vue-i18n": "^9.1.7", "vue-router": "^4.0.11", diff --git a/src/libs/vditor/src/assets/scss/_content.scss b/src/libs/vditor/src/assets/scss/_content.scss new file mode 100644 index 00000000..73103eb7 --- /dev/null +++ b/src/libs/vditor/src/assets/scss/_content.scss @@ -0,0 +1,284 @@ +@keyframes slideInDown { + from { + transform: translate3d(0, -100%, 0); + visibility: visible; + } + + to { + transform: translate3d(0, 0, 0); + } +} + +.vditor { + display: flex; + flex-direction: column; + border: 1px solid var(--border-color); + border-radius: 3px; + box-sizing: border-box; + font-family: $font-family-base; + + .vditor-copy { + z-index: auto; + } + + &--fullscreen { + position: fixed; + top: 0; + width: 100% !important; + left: 0; + height: 100vh !important; + z-index: 90; + border-radius: 0; + } + + &-content { + display: flex; + min-height: 60px; + flex: 1; + min-width: 1px; + position: relative; + } + + &-preview { + flex: 1; + min-width: 1px; + overflow: auto; + margin-left: -1px; + border-left: 1px solid var(--border-color); + box-sizing: border-box; + border-radius: 0 0 3px 0; + background-color: var(--textarea-background-color); + + &::-webkit-scrollbar { + display: none; + } + + &__action { + text-align: center; + padding: 10px; + background-color: var(--toolbar-background-color); + + button { + background-color: var(--toolbar-background-color); + color: var(--toolbar-icon-color); + line-height: 20px; + border: 0; + margin: 0 10px; + cursor: pointer; + padding: 0 7px; + font-size: 12px; + + &.vditor-preview__action--current, + &:hover { + color: var(--toolbar-icon-hover-color); + background-color: var(--toolbar-background-color); + } + + &:focus { + outline: none; + } + + svg { + fill: currentColor; + height: 15px; + width: 15px; + vertical-align: middle; + } + } + } + + & > .vditor-reset { + padding: 10px; + margin: 0 auto; + } + + img:not(.emoji) { + cursor: pointer; + } + } + + &-devtools { + display: none; + background-color: var(--textarea-background-color); + overflow: auto; + flex: 1; + min-width: 1px; + box-shadow: inset 1px 0 var(--border-color); + box-sizing: border-box; + border-radius: 0 0 3px 0; + padding: 10px; + } + + &-counter { + padding: 3px; + color: var(--toolbar-icon-color); + background-color: var(--count-background-color); + border-radius: 3px; + font-size: 12px; + user-select: none; + float: right; + margin: 8px 3px 0 0; + + &--error { + color: $errorColor; + background-color: rgba($errorColor, 0.1); + } + } + + &-resize { + padding: 3px 0; + cursor: row-resize; + user-select: none; + position: absolute; + width: 100%; + + &--top { + top: -3px; + } + + &--bottom { + bottom: -3px; + } + + & > div { + height: 3px; + background-color: var(--resize-background-color); + transition: $transition; + } + + &:hover, + &--selected { + & > div { + background-color: var(--resize-hover-background-color); + } + + svg { + color: var(--resize-hover-icon-color); + } + } + + svg { + fill: currentColor; + stroke-width: 0; + stroke: currentColor; + width: 13px; + height: 3px; + display: block; + margin: 0 auto; + color: var(--resize-icon-color); + } + } + + &-upload { + position: absolute; + height: 3px; + left: 0; + top: -2px; + transition: $transition; + background-color: $blurColor; + } + + &-tip { + position: absolute; + font-size: 12px; + top: 10px; + animation-duration: .15s; + animation-fill-mode: both; + left: 50%; + z-index: 5; + + &--show { + display: block; + animation-name: slideInDown; + } + + &__content { + text-align: left; + display: inline-block; + line-height: 16px; + padding: 3px 10px; + border-radius: 3px; + background: var(--toolbar-background-color); + position: relative; + margin-left: -50%; + color: var(--toolbar-icon-color); + max-width: 100%; + box-shadow: var(--panel-shadow); + + ul { + margin: 2px 0; + padding: 0 0 0 18px; + } + + a { + color: $blurColor; + } + } + + &__close { + position: absolute; + color: var(--toolbar-icon-color); + top: -7px; + right: -15px; + font-weight: bold; + cursor: pointer; + + &:hover { + color: var(--toolbar-icon-hover-color); + } + } + } + + &-img { + position: fixed; + top: 0; + left: 0; + right: 0; + bottom: 0; + display: flex; + flex-direction: column; + z-index: 3; + + &__bar { + border-bottom: 1px solid var(--border-color); + background-color: var(--toolbar-background-color); + text-align: center; + height: 36px; + box-sizing: border-box; + display: flex; + align-items: center; + justify-content: center; + } + + &__btn { + display: flex; + align-items: center; + cursor: pointer; + margin-left: 24px; + user-select: none; + color: var(--toolbar-icon-color); + + &:hover { + color: var(--toolbar-icon-hover-color); + } + + svg { + height: 14px; + width: 14px; + margin-right: 8px; + fill: currentColor; + } + } + + &__img { + flex: 1; + background-color: var(--textarea-background-color); + overflow: auto; + cursor: zoom-out; + + img { + max-width: none; + } + } + } +} diff --git a/src/libs/vditor/src/assets/scss/_hint.scss b/src/libs/vditor/src/assets/scss/_hint.scss new file mode 100644 index 00000000..d9fbbb8d --- /dev/null +++ b/src/libs/vditor/src/assets/scss/_hint.scss @@ -0,0 +1,67 @@ +.vditor-hint { + background-color: var(--panel-background-color); + position: absolute; + box-shadow: var(--panel-shadow); + border-radius: 3px; + padding: 5px 0; + z-index: 4; + line-height: 20px; + list-style: none; + font-size: 12px; + margin: 0; + max-width: 250px; + min-width: 80px; + display: none; + + .vditor-hint { + margin-top: -31px; + left: 100%; + right: auto; + + &.vditor-panel--left { + right: 100%; + left: auto; + } + } + + button { + color: var(--toolbar-icon-color); + display: block; + padding: 3px 10px; + border: 0; + border-radius: 0; + line-height: 20px; + width: 100%; + box-sizing: border-box; + text-align: left; + margin: 0; + background-color: transparent; + cursor: pointer; + white-space: nowrap; + text-overflow: ellipsis; + overflow: hidden; + + &:focus { + outline: none; + } + } + + &--current, + button:not(.vditor-menu--disabled):hover { + background-color: var(--toolbar-background-color) !important; + color: var(--toolbar-icon-hover-color) !important; + } + + &__emoji { + font-size: 16px; + float: left; + margin-right: 3px; + } + + img { + height: 20px; + width: 20px; + float: left; + margin-right: 3px; + } +} diff --git a/src/libs/vditor/src/assets/scss/_ir.scss b/src/libs/vditor/src/assets/scss/_ir.scss new file mode 100644 index 00000000..4db58471 --- /dev/null +++ b/src/libs/vditor/src/assets/scss/_ir.scss @@ -0,0 +1,275 @@ +.vditor-ir { + box-sizing: border-box; + flex: 1; + min-width: 1px; + position: relative; + width: 100%; + + &__node { + &[data-type="code-block"]:before, + &[data-type="code-block"]:after, + &[data-type="yaml-front-matter"]:before, + &[data-type="yaml-front-matter"]:after, + &[data-type="math-block"]:before, + &[data-type="math-block"]:after { + content: ' '; + color: var(--second-color); + } + + &:not(.vditor-ir__node--expand) .vditor-ir__marker { + padding: 0 !important; + } + + &:not(.vditor-ir__node--expand)[data-type="a"] { + cursor: pointer; + } + + &[data-type="link-ref"], + &[data-type="footnotes-ref"] { + color: $blurColor; + } + + &[data-type="html-block"] { + margin-bottom: 1em; + } + + .vditor-ir__marker { + width: 0; + overflow: hidden; + display: inline-block; + height: 0; + transition: $transition; + } + + &--hidden { + .vditor-ir__marker { + visibility: hidden; + } + } + + &--expand { + .vditor-ir__marker { + color: var(--second-color); + display: inline; + height: auto; + width: auto; + + &--hide { + display: none; + } + + &--heading { + color: var(--ir-heading-color); + } + + &--bi { + color: var(--ir-bi-color); + } + + &--link { + color: var(--ir-link-color); + } + + &--title { + color: var(--ir-title-color); + } + + &--bracket { + color: var(--ir-bracket-color); + text-decoration: underline; + } + + &--paren { + color: var(--ir-paren-color); + } + + &--info { + color: var(--ir-heading-color); + } + + &--pre code { + color: var(--textarea-text-color); + height: auto; + text-align: left; + } + } + + &[data-type="code-block"]:before, + &[data-type="code-block"]:after { + content: '```'; + } + + &[data-type="yaml-front-matter"]:before, + &[data-type="yaml-front-matter"]:after { + content: '---'; + } + + &[data-type="math-block"]:before, + &[data-type="math-block"]:after { + content: '$$'; + } + } + + span[data-type="code-block-open-marker"], + span[data-type="code-block-close-marker"], + span[data-type="yaml-front-matter-open-marker"], + span[data-type="yaml-front-matter-close-marker"], + span[data-type="math-block-open-marker"], + span[data-type="math-block-close-marker"] { + display: none; + } + } + + &__preview { + cursor: pointer; + white-space: initial; + min-height: 27px; + } + + &__link { + color: var(--ir-bracket-color); + text-decoration: underline; + } + + pre.vditor-reset { + background-color: var(--panel-background-color); + margin: 0; + white-space: pre-wrap; + height: 100%; + box-sizing: border-box; + + &[contenteditable="false"] { + opacity: 0.3; + cursor: not-allowed; + } + + &:empty::before { + content: attr(placeholder); + color: var(--second-color); + } + + &:focus { + outline: none; + background-color: var(--textarea-background-color); + } + + &:after { + content: ""; + height: var(--editor-bottom); + display: block; + } + + pre { + margin: 0; + } + } + + hr { + display: inline-block; + margin: 12px 0; + width: 100%; + } + + blockquote:empty::before, + pre > code:empty::before, + p:empty::before, + h1:empty::after, + h2:empty::after, + h3:empty::after, + h4:empty::after, + h5:empty::after, + h6:empty::after { + content: ' '; + } + + // block title + & .vditor-reset > h1:before, + & .vditor-reset > h2:before, + & .vditor-reset > h3:before, + & .vditor-reset > h4:before, + & .vditor-reset > h5:before, + & .vditor-reset > h6:before, + div[data-type="link-ref-defs-block"]:before, + div[data-type="footnotes-block"]:before, + .vditor-toc:before { + float: left; + padding-right: 4px; + margin-left: -29px; + content: 'H1'; + font-size: 0.85rem; + font-weight: normal; + color: var(--second-color); + } + + & .vditor-reset > h2:before { + content: 'H2'; + } + + & .vditor-reset > h3:before { + content: 'H3'; + } + + & .vditor-reset > h4:before { + content: 'H4'; + } + + & .vditor-reset > h5:before { + content: 'H5'; + } + + & .vditor-reset > h6:before { + content: 'H6'; + } + + div[data-type="link-ref-defs-block"] { + &:before { + content: '"A"'; + } + + color: var(--blockquote-color); + } + + div[data-type="footnotes-block"] { + &:before { + content: '^F'; + } + + border-top: 2px solid var(--heading-border-color); + padding-top: 24px; + margin-top: 24px; + + & > div[data-type="footnotes-def"] { + & > ul, & > ol, & > p, & > blockquote, & > pre, & > table, & > hr { + margin-left: 8px; + } + } + } + + + .vditor-toc { + &:before { + content: "ToC"; + } + + span[data-type="toc-h"] { + color: $blurColor; + text-decoration: underline; + } + } +} + +@media screen and (max-width: $max-width) { + .vditor-ir { + h1:before, + h2:before, + h3:before, + h4:before, + h5:before, + h6:before, + div[data-type="link-ref-defs-block"]:before, + div[data-type="footnotes-block"]:before, + .vditor-toc:before { + content: none; + } + } +} diff --git a/src/libs/vditor/src/assets/scss/_panel.scss b/src/libs/vditor/src/assets/scss/_panel.scss new file mode 100644 index 00000000..03f4e93c --- /dev/null +++ b/src/libs/vditor/src/assets/scss/_panel.scss @@ -0,0 +1,110 @@ +/** + * panel. + * + * @author Liyuan Li + * @version 1.0.0.0, Jan 22, 2020 + */ + +@keyframes scale-in { + 0% { + opacity: 0; + transform: scale(.5); + } + 100% { + opacity: 1; + transform: scale(1); + } +} + +.vditor { + &-panel { + background-color: var(--panel-background-color); + position: absolute; + box-shadow: var(--panel-shadow); + border-radius: 3px; + padding: 5px; + z-index: 3; + font-size: 14px; + display: none; + user-select: none; + max-width: 320px; + min-width: 80px; + animation-duration: .15s; + animation-name: scale-in; + animation-timing-function: cubic-bezier(.2, 0, .13, 1.5); + color: var(--toolbar-icon-color); + + &--none { + padding: 0; + animation: none; + min-width: auto; + max-width: none; + white-space: nowrap; + opacity: .86; + } + + &--arrow:before { + position: absolute; + width: 0; + height: 0; + pointer-events: none; + content: " "; + border: 7px solid transparent; + top: -14px; + left: 5px; + border-bottom-color: var(--panel-background-color); + } + + &--left { + right: 0; + + &.vditor-panel--arrow:before { + right: 5px; + left: auto; + } + } + } + + &-input { + border: 0; + padding: 3px 5px; + background-color: var(--panel-background-color); + font-size: 12px; + color: var(--textarea-text-color); + + &:focus { + background-color: var(--toolbar-background-color); + outline: none; + } + } + + &-icon { + color: var(--toolbar-icon-color); + cursor: pointer; + float: left; + padding: 4px 5px; + height: 21px; + width: 23px; + background-color: transparent; + border: 0; + box-sizing: border-box; + + &:hover, + &--current { + color: var(--toolbar-icon-hover-color); + background-color: transparent; + } + + &:focus { + outline: none; + } + + svg { + height: 13px !important; + width: 13px !important; + float: left; + fill: currentColor; + pointer-events: none; + } + } +} diff --git a/src/libs/vditor/src/assets/scss/_reset.scss b/src/libs/vditor/src/assets/scss/_reset.scss new file mode 100644 index 00000000..8c4e67ab --- /dev/null +++ b/src/libs/vditor/src/assets/scss/_reset.scss @@ -0,0 +1,603 @@ +.vditor { + &-reset { + &--anchor { + padding-left: 20px; + } + + &--error { + color: $errorColor; + font-size: 12px; + display: block; + line-height: 16px; + } + + color: #24292e; + font-variant-ligatures: no-common-ligatures; + font-family: $font-family-base; + word-wrap: break-word; + overflow: auto; + line-height: 1.5; + font-size: 16px; + word-break: break-word; + + ul ul ul { + list-style-type: square; + } + + ul ul { + list-style-type: circle; + } + + ul { + list-style-type: disc; + } + + ul, + ol { + padding-left: 2em; + margin-top: 0; + margin-bottom: 16px; + } + + li + li { + margin-top: 0.25em; + } + + audio { + max-width: 100%; + + &:focus { + outline: none; + } + } + + video { + max-height: 90vh; + max-width: 100%; + } + + img { + max-width: 100%; + } + + img.emoji { + cursor: auto; + max-width: 20px; + vertical-align: sub; + } + + h1, + h2, + h3, + h4, + h5, + h6 { + margin-top: 24px; + margin-bottom: 16px; + font-weight: 600; + line-height: 1.25; + + &:hover .vditor-anchor svg { + visibility: visible; + } + } + + h1 { + font-size: 1.75em; + } + + h2 { + font-size: 1.55em; + } + + h3 { + font-size: 1.38em; + } + + h4 { + font-size: 1.25em + } + + h5 { + font-size: 1.13em; + } + + h6 { + font-size: 1em; + } + + hr { + height: 2px; + padding: 0; + margin: 24px 0; + background-color: #eaecef; + border: 0; + } + + p { + margin-top: 0; + margin-bottom: 16px; + } + + blockquote { + padding: 0 1em; + color: #6a737d; + border-left: 0.25em solid #eaecef; + margin: 0 0 16px 0; + + & > :first-child { + margin-top: 0 + } + + & > :last-child { + margin-bottom: 0 + } + } + + ins > iframe { + border: 0; + } + + iframe { + border: 1px solid #d1d5da; + max-width: 100%; + box-sizing: border-box; + + &.iframe__video { + min-width: 80%; + min-height: 36vh; + } + } + + table { + border-collapse: collapse; + empty-cells: show; + margin-bottom: 16px; + overflow: auto; + border-spacing: 0; + display: block; + word-break: keep-all; + width: 100%; + + tr { + background-color: #fafbfc; + border-top: 1px solid #c6cbd1; + } + + td, + th { + padding: 6px 13px; + border: 1px solid #dfe2e5; + word-break: normal; + white-space: nowrap; + + &:first-child::after { + content: ""; + display: inline-block; + vertical-align: top; + min-height: 24px; + } + } + + th { + font-weight: 600; + } + + tbody tr:nth-child(2n) { + background-color: #fff; + } + } + + code:not(.hljs):not(.highlight-chroma) { + padding: 0.2em 0.4em; + margin: 0; + font-size: 85%; + border-radius: 3px; + font-family: $font-family-code; + word-break: break-word; + background-size: 20px 20px; + white-space: pre-wrap; + } + + pre { + margin: 1em 0; + + & > code { + margin: 0; + font-size: 85%; + padding: 0.5em; + border-radius: 5px; + display: block; + overflow: auto; + white-space: pre; + font-family: $font-family-code; + background-size: 20px 20px; + background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8AgMAAABHkjHhAAAACVBMVEWAgIBaWlo+Pj7rTFvWAAAAA3RSTlMHCAw+VhR4AAAA+klEQVQoz4WSMW7EQAhFPxKWNh2FCx+HkaZI6RRb5DYbyVfIJXLKDCFoMbaTKSw/8ZnPAPjaH2xgZcUNUDADD7D9LtDBCLZ45fbkvo/30K8yeI64pPwl6znd/3n/Oe93P3ho9qeh72btTFzqkz0rsJle8Zr81OLEwZ1dv/713uWqvu2pl+k0fy7MWtj9r/tN5q/02z89qa/L4Dc2LvM93kezPfXlME/O86EbY/V9GB9ePX8G1/6W+/9h1dq/HGfTfzT3j/xNo7522Bfnqe5jO/fvhVthlfk434v3iO9zG/UOphyPeinPl1J8Gtaa7xPTa/Dk+RIs4deMvwGvcGsmsCvJ0AAAAABJRU5ErkJggg==); + word-break: initial; + word-wrap: normal; + } + + &:hover div.vditor-copy { + display: block; + } + } + + .language-math, + .language-echarts, + .language-mindmap, + .language-plantuml, + .language-mermaid, + .language-abc, + .language-flowchart, + .language-graphviz { + margin-bottom: 16px; + } + + .language-math mjx-container:focus { + outline: none; + cursor: context-menu; + } + + .language-echarts, + .language-mindmap { + overflow: hidden; + height: 420px; + } + + .language-mermaid, + .language-flowchart, + .language-graphviz { + text-align: center; + } + + .language-graphviz parsererror { + overflow: auto; + } + + kbd { + display: inline-block; + padding: 3px 5px; + font: 11px Consolas, "Liberation Mono", Menlo, Courier, monospace; + line-height: 10px; + color: #24292e; + vertical-align: middle; + background-color: #fafbfc; + border: solid 1px #d1d5da; + border-radius: 3px; + box-shadow: inset 0 -1px 0 #d1d5da; + } + + summary { + cursor: pointer; + + &:focus { + outline: none; + } + } + + svg { + height: auto; + width: auto; + stroke-width: initial; + } + + p:last-child, + blockquote:last-child, + pre:last-child, + ul:last-child, + ol:last-child, + hr:last-child { + margin-bottom: 0; + } + } + + &-comment { + border-bottom: 2px solid rgb(248, 230, 171); + + &--focus, + &--hover { + background-color: rgb(250, 241, 209); + border-bottom: 2px solid rgb(255, 198, 10); + + .vditor-comment { + border-bottom: 2px solid rgb(255, 198, 10); + } + } + } + + &-task { + list-style: none !important; + word-break: break-all; + + input { + margin: 0 .2em .25em -1.6em; + font-size: 12px; + vertical-align: middle; + } + } + + &-copy { + position: relative; + display: none; + z-index: 1; + + textarea { + position: absolute; + left: -100000px; + height: 10px; + } + + span { + cursor: pointer; + position: absolute; + right: 15px; + top: 0.5em; + } + + svg { + color: #586069; + height: 14px; + width: 14px !important; + display: block; + fill: currentColor; + } + } + + &-linenumber { + padding-left: 4em !important; + position: relative; + + &__rows { + position: absolute; + pointer-events: none; + top: 0.5em; + left: 0; + width: 3em; /* works for line-numbers below 1000 lines */ + user-select: none; + counter-reset: linenumber; + + & > span { + pointer-events: none; + display: block; + + &::before { + counter-increment: linenumber; + content: counter(linenumber); + color: rgba(158, 150, 150, 0.38); + display: block; + padding-right: 1em; + text-align: right; + } + } + } + } + + &-speech { + position: absolute; + display: none; + background-color: #f6f8fa; + border: 1px solid #d1d5da; + border-radius: 3px; + padding: 3px; + cursor: pointer; + color: #586069; + + &:hover, + &--current { + color: #4285f4; + } + + svg { + height: 14px; + width: 14px; + fill: currentColor; + display: block; + stroke-width: 0; + stroke: currentColor; + } + } + + &-anchor { + margin-left: 5px; + + &--left { + float: left; + padding-right: 4px; + margin-left: -20px; + } + + svg { + visibility: hidden; + } + + &:hover { + svg { + visibility: visible; + } + } + + &:focus { + outline: none; + } + } + + &-linkcard { + margin: 31px auto 16px; + transition: $transition; + cursor: pointer; + max-width: 768px; + padding: 0 10px; + + a { + border-radius: 3px; + background-color: #f6f8fa; + overflow: hidden; + max-height: 250px; + display: flex; + text-decoration: none; + flex-wrap: wrap-reverse; + box-shadow: 0 1px 2px rgba(0, 0, 0, .2); + + &:hover { + box-shadow: 0 0 3px rgba(0, 0, 0, .13), 0 3px 6px rgba(0, 0, 0, .26); + text-decoration: none; + } + + &:visited .vditor-linkcard__abstract { + color: rgba(88, 96, 105, 0.36); + } + } + + &__info { + padding: 10px; + min-width: 200px; + box-sizing: border-box; + flex: 1; + } + + &__title { + font-size: 14px; + font-weight: 400; + color: #24292e; + display: flex; + align-items: center; + + img { + cursor: pointer; + height: 20px; + width: 20px; + border-radius: 3px; + flex-shrink: 0; + margin-right: 5px; + } + } + + &__abstract { + word-wrap: break-word; + word-break: break-all; + -webkit-line-clamp: 2; + overflow: hidden; + text-overflow: ellipsis; + -webkit-box-orient: vertical; + display: -webkit-box; + font-size: 13px; + color: #586069; + margin: 5px 0; + } + + &__site { + font-size: 12px; + color: #4285f4; + } + + &__image { + background-size: cover; + background-repeat: no-repeat; + background-position: center center; + max-width: 250px; + min-width: 126px; + cursor: pointer; + background-color: rgba(88, 96, 105, 0.36); + } + } + + &-footnotes__goto-ref { + text-decoration: none; + } + + &-toc { + margin-bottom: 16px; + user-select: text; + color: $blurColor; + + .vditor-outline__action { + display: none; + } + + ul { + list-style: none !important; + padding-left: 1em; + } + + & > ul { + padding-left: 0; + } + + span { + cursor: pointer; + } + + li > span > svg { + width: 0; + height: 0; + } + } + + &-outline { + width: 250px; + border-right: 1px solid var(--border-color); + background-color: var(--panel-background-color); + display: none; + overflow: auto; + + &--right { + border-right: 0; + border-left: 1px solid var(--border-color); + } + + &::-webkit-scrollbar { + display: none; + } + + ul { + list-style: none !important; + padding-left: 1em; + margin: 0; + } + + &__content > ul { + padding-left: 0; + } + + li > span { + display: flex; + align-items: center; + padding: 5px 10px; + cursor: pointer; + color: var(--textarea-text-color); + + & > svg { + height: 10px; + width: 10px; + } + + &:hover { + color: var(--toolbar-icon-hover-color); + } + + & > span { + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + } + } + + &__title { + border-bottom: 1px dashed var(--border-color); + padding: 5px 10px; + color: var(--toolbar-icon-color); + font-size: 12px; + } + + &__action { + transition: $transition; + fill: currentColor; + margin-right: 5px; + flex-shrink: 0; + + &--close { + transform: rotate(-90deg); + } + } + } +} diff --git a/src/libs/vditor/src/assets/scss/_sv.scss b/src/libs/vditor/src/assets/scss/_sv.scss new file mode 100644 index 00000000..ff1e1089 --- /dev/null +++ b/src/libs/vditor/src/assets/scss/_sv.scss @@ -0,0 +1,133 @@ +.vditor-sv { + + font-family: $font-family-base; + margin: 0 1px 0 0; + overflow: auto; + width: 100%; + flex: 1; + min-width: 1px; + border: 0; + resize: none; + padding: 10px 9px 10px 10px; + box-sizing: border-box; + background-color: var(--panel-background-color); + outline: 0 none; + font-size: 16px; + line-height: 22px; + color: var(--textarea-text-color); + border-radius: 0 0 3px 3px; + font-variant-ligatures: no-common-ligatures; + white-space: pre-wrap; + word-break: break-word; + word-wrap: break-word; + + &[contenteditable="false"] { + opacity: 0.3; + cursor: not-allowed; + } + + &:empty::before { + content: attr(placeholder); + color: var(--second-color); + } + + &:focus { + background-color: var(--textarea-background-color); + } + + &:after { + content: ""; + height: var(--editor-bottom); + display: block; + } + + span[data-type="newline"] + span[data-type="text"]:empty { + display: inherit; + } + + .sup { + vertical-align: super; + font-size: smaller; + } + + .strong { + font-weight: bold; + } + + .em { + font-style: italic; + } + + .s { + text-decoration: line-through; + } + + .mark:not(.vditor-sv__marker) { + background-color: yellow; + color: black; + } + + .h1 { + font-size: 1.75em; + line-height: 44px; + } + + .h2 { + font-size: 1.55em; + line-height: 38px; + } + + .h3 { + font-size: 1.38em; + line-height: 27px; + } + + .h4 { + font-size: 1.25em; + line-height: 25px; + } + + .h5 { + font-size: 1.13em; + } + + .h6 { + font-size: 1em; + } + + &__marker { + color: var(--second-color); + + &--heading { + color: var(--ir-heading-color); + } + + &--bi { + color: var(--ir-bi-color); + } + + &--link { + color: var(--ir-link-color); + } + + &--title { + color: var(--ir-title-color); + } + + &--bracket { + color: var(--ir-bracket-color); + } + + &--paren { + color: var(--ir-paren-color); + } + + &--info { + color: var(--ir-heading-color); + } + + &--strong { + font-weight: bold; + } + } +} diff --git a/src/libs/vditor/src/assets/scss/_toolbar.scss b/src/libs/vditor/src/assets/scss/_toolbar.scss new file mode 100644 index 00000000..708bdf01 --- /dev/null +++ b/src/libs/vditor/src/assets/scss/_toolbar.scss @@ -0,0 +1,180 @@ +.vditor { + &-toolbar { + background-color: var(--toolbar-background-color); + border-bottom: 1px solid var(--border-color); + padding: 0 5px; + line-height: 1; + + &--pin { + position: sticky; + top: 0; + z-index: 1; + } + + &--hide { + transition: $transition; + height: 5px; + overflow: hidden; + + &:hover { + background-color: var(--toolbar-background-color); + height: auto; + overflow: visible; + } + } + + &__item { + float: left; + position: relative; + + .vditor-tooltipped { + color: var(--toolbar-icon-color); + border: 0; + padding: 10px 5px; + background-color: transparent; + height: var(--toolbar-height); + width: 25px; + box-sizing: border-box; + font-size: 0; + + &:focus { + outline: none; + } + + &:focus { + cursor: pointer; + color: var(--toolbar-icon-hover-color); + } + } + + svg { + fill: currentColor; + display: inline-block; + stroke-width: 0; + stroke: currentColor; + width: 15px; + height: 15px; + } + + input { + position: absolute; + width: 25px; + height: var(--toolbar-height); + top: 0; + left: 0; + cursor: pointer; + opacity: .001; + overflow: hidden; + } + } + + &__divider { + float: left; + height: calc(var(--toolbar-height) - (var(--toolbar-divider-margin-top) * 2)); + border-left: 1px solid var(--second-color); + margin: var(--toolbar-divider-margin-top) 8px; + } + + &__br { + width: 100%; + padding: 0 !important; + height: 0 !important; + } + } + + &-menu { + &--current { + color: var(--toolbar-icon-hover-color) !important; + } + + &--disabled { + color: var(--second-color) !important; + cursor: not-allowed !important; + } + } + + &-emojis { + display: inline-block; + overflow: auto; + + &::-webkit-scrollbar { + display: none; + } + + &__tip { + flex: 1; + min-width: 1px; + width: 200px; + margin-right: 10px; + color: var(--toolbar-icon-color); + white-space: nowrap; + text-overflow: ellipsis; + overflow: hidden; + } + + &__tail { + margin-top: 5px; + font-size: 12px; + color: var(--toolbar-icon-color); + display: flex; + + a { + text-decoration: none; + color: var(--toolbar-icon-color); + + &:hover { + color: var(--toolbar-icon-hover-color); + } + } + } + + button { + cursor: pointer; + border-radius: 3px; + float: left; + height: 30px; + width: 30px; + text-align: center; + line-height: 26px; + padding: 3px; + box-sizing: border-box; + font-size: 16px; + transition: $transition; + border: 0; + margin: 0; + background-color: transparent; + overflow: hidden; + + &:focus { + outline: none; + } + + &:hover .vditor-emojis__icon { + display: inline-block; + transform: scale(1.2); + } + } + + img { + height: 20px; + width: 20px; + float: left; + margin: 3px 0 0 3px; + } + } +} + +@media screen and (max-width: $max-width) { + .vditor-toolbar__item { + padding: 0 12px; + } + .vditor-panel--left.vditor-panel--arrow:before { + right: 17px; + } +} + +@media(hover: hover) and (pointer: fine) { + .vditor-toolbar__item .vditor-tooltipped:hover { + color: var(--toolbar-icon-hover-color); + } +} diff --git a/src/libs/vditor/src/assets/scss/_tooltipped.scss b/src/libs/vditor/src/assets/scss/_tooltipped.scss new file mode 100644 index 00000000..1e7980ae --- /dev/null +++ b/src/libs/vditor/src/assets/scss/_tooltipped.scss @@ -0,0 +1,171 @@ +/** + * tool tip. + * + * @author Liyuan Li + * @version 0.1.0.0, Jan 25, 2019 + */ +@keyframes tooltip-appear { + from { + opacity: 0 + } + + to { + opacity: 1 + } +} + +.vditor-tooltipped { + position: relative; + cursor: pointer; + + &::after { + position: absolute; + z-index: 1000000; + display: none; + padding: 5px 8px; + font-size: 11px; + font-weight: normal; + -webkit-font-smoothing: subpixel-antialiased; + color: #fff; + text-align: center; + text-decoration: none; + text-shadow: none; + text-transform: none; + letter-spacing: normal; + word-wrap: break-word; + white-space: pre; + pointer-events: none; + content: attr(aria-label); + background: #3b3e43; + border-radius: 3px; + line-height: 16px; + opacity: 0 + } + + &::before { + position: absolute; + z-index: 1000001; + display: none; + width: 0; + height: 0; + color: #3b3e43; + pointer-events: none; + content: ""; + border: 5px solid transparent; + opacity: 0 + } + + &--hover::before, + &--hover::after, + &:hover::before, + &:hover::after, + &:active::before, + &:active::after, + &:focus::before, + &:focus::after { + display: inline-block; + text-decoration: none; + animation-name: tooltip-appear; + animation-duration: 0.15s; + animation-fill-mode: forwards; + animation-timing-function: ease-in; + } + + &__s::after, + &__se::after, + &__sw::after { + top: 100%; + right: 50%; + margin-top: 5px + } + + &__s::before, + &__se::before, + &__sw::before { + top: auto; + right: 50%; + bottom: -5px; + margin-right: -5px; + border-bottom-color: #3b3e43 + } + + &__se::after { + right: auto; + left: 50%; + margin-left: -15px + } + + &__sw::after { + margin-right: -15px + } + + &__n::after, + &__ne::after, + &__nw::after { + right: 50%; + bottom: 100%; + margin-bottom: 5px + } + + &__n::before, + &__ne::before, + &__nw::before { + top: -5px; + right: 50%; + bottom: auto; + margin-right: -5px; + border-top-color: #3b3e43 + } + + &__ne::after { + right: auto; + left: 50%; + margin-left: -15px + } + + &__nw::after { + margin-right: -15px + } + + &__s::after, + &__n::after { + transform: translateX(50%) + } + + &__w::after { + right: 100%; + bottom: 50%; + margin-right: 5px; + transform: translateY(50%); + } + + &__w::before { + top: 50%; + bottom: 50%; + left: -5px; + margin-top: -5px; + border-left-color: #3b3e43; + } + + &__e::after { + bottom: 50%; + left: 100%; + margin-left: 5px; + transform: translateY(50%) + } + + &__e::before { + top: 50%; + right: -5px; + bottom: 50%; + margin-top: -5px; + border-right-color: #3b3e43; + } +} + +@media screen and (max-width: $max-width) { + .vditor-tooltipped:before, + .vditor-tooltipped:after { + content: none; + } +} diff --git a/src/libs/vditor/src/assets/scss/_wysiwyg.scss b/src/libs/vditor/src/assets/scss/_wysiwyg.scss new file mode 100644 index 00000000..58207122 --- /dev/null +++ b/src/libs/vditor/src/assets/scss/_wysiwyg.scss @@ -0,0 +1,195 @@ +.vditor-wysiwyg { + box-sizing: border-box; + flex: 1; + position: relative; + width: 100%; + min-width: 1px; + + pre.vditor-reset { + background-color: var(--panel-background-color); + margin: 0; + white-space: pre-wrap; + height: 100%; + box-sizing: border-box; + + &[contenteditable="false"] { + opacity: 0.3; + cursor: not-allowed; + } + + &:empty::before { + content: attr(placeholder); + color: var(--second-color); + } + + &:focus { + outline: none; + background-color: var(--textarea-background-color); + } + + &:after { + content: ""; + height: var(--editor-bottom); + display: block; + } + } + + blockquote:empty::before, + pre > code:empty::before, + p:empty::before, + h1:empty::after, + h2:empty::after, + h3:empty::after, + h4:empty::after, + h5:empty::after, + h6:empty::after { + content: ' '; + } + + code[data-marker="`"] { + padding-left: 0 !important; + padding-right: 0 !important; + } + + &__block { + pre:first-child { + margin-bottom: -1em; + + code { + height: auto; + color: var(--textarea-text-color); + height: auto; + text-align: left; + } + } + + pre:last-child { + margin-bottom: 1em; + } + } + + &__preview { + cursor: pointer; + white-space: initial; + min-height: 27px; + } + + & > .vditor-reset > h1:before, + & > .vditor-reset > h2:before, + & > .vditor-reset > h3:before, + & > .vditor-reset > h4:before, + & > .vditor-reset > h5:before, + & > .vditor-reset > h6:before, + div.vditor-wysiwyg__block:before, + div[data-type="link-ref-defs-block"]:before, + div[data-type="footnotes-block"]:before, + .vditor-toc:before { + float: left; + padding-right: 4px; + margin-left: -29px; + content: 'H1'; + font-size: 0.85rem; + font-weight: normal; + color: var(--second-color); + } + + & > .vditor-reset > h2:before { + content: 'H2'; + } + + & > .vditor-reset > h3:before { + content: 'H3'; + } + + & > .vditor-reset > h4:before { + content: 'H4'; + } + + & > .vditor-reset > h5:before { + content: 'H5'; + } + + & > .vditor-reset > h6:before { + content: 'H6'; + } + + div[data-type="link-ref-defs-block"]:before { + content: '"A"'; + } + + div[data-type="footnotes-block"]:before { + content: '^F'; + } + + div.vditor-wysiwyg__block:before { + content: ">"; + } + + div.vditor-wysiwyg__block[data-type="yaml-front-matter"]:before { + content: "F"; + } + + div.vditor-wysiwyg__block[data-type="math-block"]:before { + content: "$$"; + } + + .vditor-toc:before { + content: "ToC"; + } + + hr { + display: inline-block; + margin: 12px 0; + width: 100%; + } + + details { + white-space: initial; + } + + a { + cursor: pointer; + } + + span[data-type="backslash"] > span { + display: none; + color: var(--second-color); + } + + span[data-type="link-ref"], + sup[data-type="footnotes-ref"] { + color: $blurColor; + } + + span[data-type="toc-h"] { + color: $blurColor; + text-decoration: underline; + } + + div[data-type="footnotes-block"] { + border-top: 2px solid var(--heading-border-color); + padding-top: 24px; + margin-top: 24px; + } + + div[data-type="link-ref-defs-block"] { + color: var(--blockquote-color); + } +} + +@media screen and (max-width: $max-width) { + .vditor-wysiwyg { + h1:before, + h2:before, + h3:before, + h4:before, + h5:before, + h6:before, + div.vditor-wysiwyg__block:before, + div[data-type="link-ref-defs-block"]:before, + div[data-type="footnotes-block"]:before, + .vditor-toc:before { + content: none; + } + } +} diff --git a/src/libs/vditor/src/assets/scss/index.scss b/src/libs/vditor/src/assets/scss/index.scss new file mode 100755 index 00000000..f7f462db --- /dev/null +++ b/src/libs/vditor/src/assets/scss/index.scss @@ -0,0 +1,83 @@ +$errorColor: #d23f31 !default; +$blurColor: #4285f4 !default; + +$font-family-base: "Helvetica Neue", "Luxi Sans", "DejaVu Sans", "Hiragino Sans GB", "Microsoft Yahei", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", "Segoe UI Symbol", "Android Emoji", "EmojiSymbols" !default; +$font-family-code: mononoki, Consolas, "Liberation Mono", Menlo, Courier, monospace, "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", "Segoe UI Symbol", "Android Emoji", "EmojiSymbols" !default; + +$transition: all .15s ease-in-out !default; + +$max-width: 520px !default; + +.vditor { + --border-color: #d1d5da; + --second-color: rgba(88, 96, 105, 0.36); + + --panel-background-color: #fff; + --panel-shadow: 0 1px 2px rgba(0, 0, 0, .2); + + --toolbar-background-color: #f6f8fa; + --toolbar-icon-color: #586069; + --toolbar-icon-hover-color: #{$blurColor}; + --toolbar-height: 35px; + --toolbar-divider-margin-top: 8px; + + --textarea-background-color: #fafbfc; + --textarea-text-color: #24292e; + + --resize-icon-color: var(--toolbar-icon-color); + --resize-background-color: var(--toolbar-background-color); + --resize-hover-icon-color: var(--panel-background-color); + --resize-hover-background-color: var(--toolbar-icon-hover-color); + + --count-background-color:rgba(27, 31, 35, .05); + --heading-border-color: #eaecef; + --blockquote-color: #6a737d; + + --ir-heading-color: #660e7a; + --ir-title-color: #808080; + --ir-bi-color: #0033b3; + --ir-link-color: #008000; + --ir-bracket-color: #0000ff; + --ir-paren-color: #008000; + + &--dark { + --border-color: #141414; + --second-color: rgba(185, 185, 185, .36); + + --panel-background-color: #24292e; + --panel-shadow: 0 1px 2px rgba(255, 255, 255, .2); + + --toolbar-background-color: #1d2125; + --toolbar-icon-color: #b9b9b9; + --toolbar-icon-hover-color: #fff; + + --textarea-background-color: #2f363d; + --textarea-text-color: #d1d5da; + + --resize-icon-color: var(--border-color); + --resize-background-color: var(--second-color); + --resize-hover-icon-color: var(--toolbar-icon-hover-color); + --resize-hover-background-color: rgba(185, 185, 185, .86); + + --count-background-color: #{rgba($blurColor, .36)}; + --heading-border-color: var(--textarea-text-color); + --blockquote-color: var(--toolbar-icon-color); + + --ir-heading-color: #9876aa; + --ir-title-color: #808080; + --ir-bi-color: #cc7832; + --ir-link-color: #ffc66d; + --ir-bracket-color: #287bde; + --ir-paren-color: #6a8759; + } +} + +@import "tooltipped"; +@import "panel"; +@import "toolbar"; +@import "content"; +@import "hint"; +@import "reset"; +@import "wysiwyg"; +@import "ir"; +@import "sv"; diff --git a/src/libs/vditor/src/index.ts b/src/libs/vditor/src/index.ts new file mode 100644 index 00000000..b34dadc5 --- /dev/null +++ b/src/libs/vditor/src/index.ts @@ -0,0 +1,502 @@ +import "./assets/scss/index.scss"; +import VditorMethod from "./method"; +import {Constants, VDITOR_VERSION} from "./ts/constants"; +import {DevTools} from "./ts/devtools/index"; +import {Hint} from "./ts/hint/index"; +import {IR} from "./ts/ir/index"; +import {input as irInput} from "./ts/ir/input"; +import {processAfterRender} from "./ts/ir/process"; +import {getHTML} from "./ts/markdown/getHTML"; +import {getMarkdown} from "./ts/markdown/getMarkdown"; +import {setLute} from "./ts/markdown/setLute"; +import {Outline} from "./ts/outline/index"; +import {Preview} from "./ts/preview/index"; +import {Resize} from "./ts/resize/index"; +import {Editor} from "./ts/sv/index"; +import {inputEvent} from "./ts/sv/inputEvent"; +import {processAfterRender as processSVAfterRender} from "./ts/sv/process"; +import {Tip} from "./ts/tip/index"; +import {Toolbar} from "./ts/toolbar/index"; +import {disableToolbar, hidePanel} from "./ts/toolbar/setToolbar"; +import {enableToolbar} from "./ts/toolbar/setToolbar"; +import {initUI} from "./ts/ui/initUI"; +import {setCodeTheme} from "./ts/ui/setCodeTheme"; +import {setContentTheme} from "./ts/ui/setContentTheme"; +import {setPreviewMode} from "./ts/ui/setPreviewMode"; +import {setTheme} from "./ts/ui/setTheme"; +import {Undo} from "./ts/undo/index"; +import {Upload} from "./ts/upload/index"; +import {addScript, addScriptSync} from "./ts/util/addScript"; +import {getSelectText} from "./ts/util/getSelectText"; +import {Options} from "./ts/util/Options"; +import {processCodeRender} from "./ts/util/processCode"; +import {getCursorPosition, getEditorRange} from "./ts/util/selection"; +import {afterRenderEvent} from "./ts/wysiwyg/afterRenderEvent"; +import {WYSIWYG} from "./ts/wysiwyg/index"; +import {input} from "./ts/wysiwyg/input"; +import {renderDomByMd} from "./ts/wysiwyg/renderDomByMd"; + +class Vditor extends VditorMethod { + public readonly version: string; + public vditor: IVditor; + + /** + * @param id 要挂载 Vditor 的元素或者元素 ID。 + * @param options Vditor 参数 + */ + constructor(id: string | HTMLElement, options?: IOptions) { + super(); + this.version = VDITOR_VERSION; + + if (typeof id === "string") { + if (!options) { + options = { + cache: { + id: `vditor${id}`, + }, + }; + } else if (!options.cache) { + options.cache = {id: `vditor${id}`}; + } else if (!options.cache.id) { + options.cache.id = `vditor${id}`; + } + id = document.getElementById(id); + } + + const getOptions = new Options(options); + const mergedOptions = getOptions.merge(); + + // 支持自定义国际化 + if (!mergedOptions.i18n) { + if (!["en_US", "ja_JP", "ko_KR", "ru_RU", "zh_CN", "zh_TW"].includes(mergedOptions.lang)) { + throw new Error( + "options.lang error, see https://ld246.com/article/1549638745630#options", + ); + } else { + addScript(`${mergedOptions.cdn}/dist/js/i18n/${mergedOptions.lang}.js`, "vditorI18nScript").then(() => { + this.init(id as HTMLElement, mergedOptions); + }); + } + } else { + window.VditorI18n = mergedOptions.i18n; + this.init(id, mergedOptions); + } + } + + /** 设置主题 */ + public setTheme( + theme: "dark" | "classic", + contentTheme?: string, + codeTheme?: string, + contentThemePath?: string, + ) { + this.vditor.options.theme = theme; + setTheme(this.vditor); + if (contentTheme) { + this.vditor.options.preview.theme.current = contentTheme; + setContentTheme(contentTheme, contentThemePath || this.vditor.options.preview.theme.path); + } + if (codeTheme) { + this.vditor.options.preview.hljs.style = codeTheme; + setCodeTheme(codeTheme, this.vditor.options.cdn); + } + } + + /** 获取 Markdown 内容 */ + public getValue() { + return getMarkdown(this.vditor); + } + + /** 获取编辑器当前编辑模式 */ + public getCurrentMode() { + return this.vditor.currentMode; + } + + /** 聚焦到编辑器 */ + public focus() { + if (this.vditor.currentMode === "sv") { + this.vditor.sv.element.focus(); + } else if (this.vditor.currentMode === "wysiwyg") { + this.vditor.wysiwyg.element.focus(); + } else if (this.vditor.currentMode === "ir") { + this.vditor.ir.element.focus(); + } + } + + /** 让编辑器失焦 */ + public blur() { + if (this.vditor.currentMode === "sv") { + this.vditor.sv.element.blur(); + } else if (this.vditor.currentMode === "wysiwyg") { + this.vditor.wysiwyg.element.blur(); + } else if (this.vditor.currentMode === "ir") { + this.vditor.ir.element.blur(); + } + } + + /** 禁用编辑器 */ + public disabled() { + hidePanel(this.vditor, ["subToolbar", "hint", "popover"]); + disableToolbar( + this.vditor.toolbar.elements, + Constants.EDIT_TOOLBARS.concat(["undo", "redo", "fullscreen", "edit-mode"]), + ); + this.vditor[this.vditor.currentMode].element.setAttribute( + "contenteditable", + "false", + ); + } + + /** 解除编辑器禁用 */ + public enable() { + enableToolbar( + this.vditor.toolbar.elements, + Constants.EDIT_TOOLBARS.concat(["undo", "redo", "fullscreen", "edit-mode"]), + ); + this.vditor.undo.resetIcon(this.vditor); + this.vditor[this.vditor.currentMode].element.setAttribute("contenteditable", "true"); + } + + /** 返回选中的字符串 */ + public getSelection() { + if (this.vditor.currentMode === "wysiwyg") { + return getSelectText(this.vditor.wysiwyg.element); + } else if (this.vditor.currentMode === "sv") { + return getSelectText(this.vditor.sv.element); + } else if (this.vditor.currentMode === "ir") { + return getSelectText(this.vditor.ir.element); + } + } + + /** 设置预览区域内容 */ + public renderPreview(value?: string) { + this.vditor.preview.render(this.vditor, value); + } + + /** 获取焦点位置 */ + public getCursorPosition() { + return getCursorPosition(this.vditor[this.vditor.currentMode].element); + } + + /** 上传是否还在进行中 */ + public isUploading() { + return this.vditor.upload.isUploading; + } + + /** 清除缓存 */ + public clearCache() { + localStorage.removeItem(this.vditor.options.cache.id); + } + + /** 禁用缓存 */ + public disabledCache() { + this.vditor.options.cache.enable = false; + } + + /** 启用缓存 */ + public enableCache() { + if (!this.vditor.options.cache.id) { + throw new Error( + "need options.cache.id, see https://ld246.com/article/1549638745630#options", + ); + } + this.vditor.options.cache.enable = true; + } + + /** HTML 转 md */ + public html2md(value: string) { + return this.vditor.lute.HTML2Md(value); + } + + /** markdown 转 JSON 输出 */ + public exportJSON(value: string) { + return this.vditor.lute.RenderJSON(value); + } + + /** 获取 HTML */ + public getHTML() { + return getHTML(this.vditor); + } + + /** 消息提示。time 为 0 将一直显示 */ + public tip(text: string, time?: number) { + this.vditor.tip.show(text, time); + } + + /** 设置预览模式 */ + public setPreviewMode(mode: "both" | "editor") { + setPreviewMode(mode, this.vditor); + } + + /** 删除选中内容 */ + public deleteValue() { + if (window.getSelection().isCollapsed) { + return; + } + document.execCommand("delete", false); + } + + /** 更新选中内容 */ + public updateValue(value: string) { + document.execCommand("insertHTML", false, value); + } + + /** 在焦点处插入内容,并默认进行 Markdown 渲染 */ + public insertValue(value: string, render = true) { + const range = getEditorRange(this.vditor); + range.collapse(true); + const tmpElement = document.createElement("template"); + tmpElement.innerHTML = value; + range.insertNode(tmpElement.content.cloneNode(true)); + if (this.vditor.currentMode === "sv") { + this.vditor.sv.preventInput = true; + if (render) { + inputEvent(this.vditor); + } + } else if (this.vditor.currentMode === "wysiwyg") { + this.vditor.wysiwyg.preventInput = true; + if (render) { + input(this.vditor, getSelection().getRangeAt(0)); + } + } else if (this.vditor.currentMode === "ir") { + this.vditor.ir.preventInput = true; + if (render) { + irInput(this.vditor, getSelection().getRangeAt(0), true); + } + } + } + + /** 设置编辑器内容 */ + public setValue(markdown: string, clearStack = false) { + if (this.vditor.currentMode === "sv") { + this.vditor.sv.element.innerHTML = this.vditor.lute.SpinVditorSVDOM(markdown); + processSVAfterRender(this.vditor, { + enableAddUndoStack: true, + enableHint: false, + enableInput: false, + }); + } else if (this.vditor.currentMode === "wysiwyg") { + renderDomByMd(this.vditor, markdown, { + enableAddUndoStack: true, + enableHint: false, + enableInput: false, + }); + } else { + this.vditor.ir.element.innerHTML = this.vditor.lute.Md2VditorIRDOM(markdown); + this.vditor.ir.element + .querySelectorAll(".vditor-ir__preview[data-render='2']") + .forEach((item: HTMLElement) => { + processCodeRender(item, this.vditor); + }); + processAfterRender(this.vditor, { + enableAddUndoStack: true, + enableHint: false, + enableInput: false, + }); + } + + this.vditor.outline.render(this.vditor); + + if (!markdown) { + hidePanel(this.vditor, ["emoji", "headings", "submenu", "hint"]); + if (this.vditor.wysiwyg.popover) { + this.vditor.wysiwyg.popover.style.display = "none"; + } + this.clearCache(); + } + if (clearStack) { + this.clearStack(); + } + } + + /** 清空 undo & redo 栈 */ + public clearStack() { + this.vditor.undo.clearStack(this.vditor); + this.vditor.undo.addToUndoStack(this.vditor); + } + + /** 销毁编辑器 */ + public destroy() { + this.vditor.element.innerHTML = this.vditor.originalInnerHTML; + this.vditor.element.classList.remove("vditor"); + this.vditor.element.removeAttribute("style"); + document.getElementById("vditorIconScript").remove(); + this.clearCache(); + } + + /** 获取评论 ID */ + public getCommentIds() { + if (this.vditor.currentMode !== "wysiwyg") { + return []; + } + return this.vditor.wysiwyg.getComments(this.vditor, true); + } + + /** 高亮评论 */ + public hlCommentIds(ids: string[]) { + if (this.vditor.currentMode !== "wysiwyg") { + return; + } + const hlItem = (item: Element) => { + item.classList.remove("vditor-comment--hover"); + ids.forEach((id) => { + if (item.getAttribute("data-cmtids").indexOf(id) > -1) { + item.classList.add("vditor-comment--hover"); + } + }); + }; + this.vditor.wysiwyg.element + .querySelectorAll(".vditor-comment") + .forEach((item) => { + hlItem(item); + }); + if (this.vditor.preview.element.style.display !== "none") { + this.vditor.preview.element + .querySelectorAll(".vditor-comment") + .forEach((item) => { + hlItem(item); + }); + } + } + + /** 取消评论高亮 */ + public unHlCommentIds(ids: string[]) { + if (this.vditor.currentMode !== "wysiwyg") { + return; + } + const unHlItem = (item: Element) => { + ids.forEach((id) => { + if (item.getAttribute("data-cmtids").indexOf(id) > -1) { + item.classList.remove("vditor-comment--hover"); + } + }); + }; + this.vditor.wysiwyg.element + .querySelectorAll(".vditor-comment") + .forEach((item) => { + unHlItem(item); + }); + if (this.vditor.preview.element.style.display !== "none") { + this.vditor.preview.element + .querySelectorAll(".vditor-comment") + .forEach((item) => { + unHlItem(item); + }); + } + } + + /** 删除评论 */ + public removeCommentIds(removeIds: string[]) { + if (this.vditor.currentMode !== "wysiwyg") { + return; + } + + const removeItem = (item: Element, removeId: string) => { + const ids = item.getAttribute("data-cmtids").split(" "); + ids.find((id, index) => { + if (id === removeId) { + ids.splice(index, 1); + return true; + } + }); + if (ids.length === 0) { + item.outerHTML = item.innerHTML; + getEditorRange(this.vditor).collapse(true); + } else { + item.setAttribute("data-cmtids", ids.join(" ")); + } + }; + removeIds.forEach((removeId) => { + this.vditor.wysiwyg.element + .querySelectorAll(".vditor-comment") + .forEach((item) => { + removeItem(item, removeId); + }); + if (this.vditor.preview.element.style.display !== "none") { + this.vditor.preview.element + .querySelectorAll(".vditor-comment") + .forEach((item) => { + removeItem(item, removeId); + }); + } + }); + afterRenderEvent(this.vditor, { + enableAddUndoStack: true, + enableHint: false, + enableInput: false, + }); + } + + private init(id: HTMLElement, mergedOptions: IOptions) { + this.vditor = { + currentMode: mergedOptions.mode, + element: id, + hint: new Hint(mergedOptions.hint.extend), + lute: undefined, + options: mergedOptions, + originalInnerHTML: id.innerHTML, + outline: new Outline(window.VditorI18n.outline), + tip: new Tip(), + }; + + this.vditor.sv = new Editor(this.vditor); + this.vditor.undo = new Undo(); + this.vditor.wysiwyg = new WYSIWYG(this.vditor); + this.vditor.ir = new IR(this.vditor); + this.vditor.toolbar = new Toolbar(this.vditor); + + if (mergedOptions.resize.enable) { + this.vditor.resize = new Resize(this.vditor); + } + + if (this.vditor.toolbar.elements.devtools) { + this.vditor.devtools = new DevTools(); + } + + if (mergedOptions.upload.url || mergedOptions.upload.handler) { + this.vditor.upload = new Upload(); + } + + addScript( + mergedOptions._lutePath || + `${mergedOptions.cdn}/dist/js/lute/lute.min.js`, + "vditorLuteScript", + ).then(() => { + this.vditor.lute = setLute({ + autoSpace: this.vditor.options.preview.markdown.autoSpace, + codeBlockPreview: this.vditor.options.preview.markdown + .codeBlockPreview, + emojiSite: this.vditor.options.hint.emojiPath, + emojis: this.vditor.options.hint.emoji, + fixTermTypo: this.vditor.options.preview.markdown.fixTermTypo, + footnotes: this.vditor.options.preview.markdown.footnotes, + headingAnchor: false, + inlineMathDigit: this.vditor.options.preview.math.inlineDigit, + linkBase: this.vditor.options.preview.markdown.linkBase, + linkPrefix: this.vditor.options.preview.markdown.linkPrefix, + listStyle: this.vditor.options.preview.markdown.listStyle, + mark: this.vditor.options.preview.markdown.mark, + mathBlockPreview: this.vditor.options.preview.markdown + .mathBlockPreview, + paragraphBeginningSpace: this.vditor.options.preview.markdown + .paragraphBeginningSpace, + sanitize: this.vditor.options.preview.markdown.sanitize, + toc: this.vditor.options.preview.markdown.toc, + }); + + this.vditor.preview = new Preview(this.vditor); + + initUI(this.vditor); + + if (mergedOptions.after) { + mergedOptions.after(); + } + if (mergedOptions.icon) { + // 防止初始化 2 个编辑器时加载 2 次 + addScriptSync(`${mergedOptions.cdn}/dist/js/icons/${mergedOptions.icon}.js`, "vditorIconScript"); + } + }); + } +} + +export default Vditor; diff --git a/src/libs/vditor/src/method.ts b/src/libs/vditor/src/method.ts new file mode 100644 index 00000000..77abcf28 --- /dev/null +++ b/src/libs/vditor/src/method.ts @@ -0,0 +1,65 @@ +import {abcRender} from "./ts/markdown/abcRender"; +import * as adapterRender from "./ts/markdown/adapterRender"; +import {chartRender} from "./ts/markdown/chartRender"; +import {codeRender} from "./ts/markdown/codeRender"; +import {flowchartRender} from "./ts/markdown/flowchartRender"; +import {graphvizRender} from "./ts/markdown/graphvizRender"; +import {highlightRender} from "./ts/markdown/highlightRender"; +import {lazyLoadImageRender} from "./ts/markdown/lazyLoadImageRender"; +import {mathRender} from "./ts/markdown/mathRender"; +import {mediaRender} from "./ts/markdown/mediaRender"; +import {mermaidRender} from "./ts/markdown/mermaidRender"; +import {mindmapRender} from "./ts/markdown/mindmapRender"; +import {outlineRender} from "./ts/markdown/outlineRender"; +import {plantumlRender} from "./ts/markdown/plantumlRender"; +import {md2html, previewRender} from "./ts/markdown/previewRender"; +import {speechRender} from "./ts/markdown/speechRender"; +import {previewImage} from "./ts/preview/image"; +import {setCodeTheme} from "./ts/ui/setCodeTheme"; +import {setContentTheme} from "./ts/ui/setContentTheme"; + +class Vditor { + + /** 点击图片放大 */ + public static adapterRender = adapterRender; + /** 点击图片放大 */ + public static previewImage = previewImage; + /** 为 element 中的代码块添加复制按钮 */ + public static codeRender = codeRender; + /** 对 graphviz 进行渲染 */ + public static graphvizRender = graphvizRender; + /** 为 element 中的代码块进行高亮渲染 */ + public static highlightRender = highlightRender; + /** 对数学公式进行渲染 */ + public static mathRender = mathRender; + /** 流程图/时序图/甘特图渲染 */ + public static mermaidRender = mermaidRender; + /** flowchart.js 渲染 */ + public static flowchartRender = flowchartRender; + /** 图表渲染 */ + public static chartRender = chartRender; + /** 五线谱渲染 */ + public static abcRender = abcRender; + /** 脑图渲染 */ + public static mindmapRender = mindmapRender; + /** plantuml渲染 */ + public static plantumlRender = plantumlRender; + /** 大纲渲染 */ + public static outlineRender = outlineRender; + /** 为[特定链接](https://github.com/Vanessa219/vditor/issues/7)分别渲染为视频、音频、嵌入的 iframe */ + public static mediaRender = mediaRender; + /** 对选中的文字进行阅读 */ + public static speechRender = speechRender; + /** 对图片进行懒加载 */ + public static lazyLoadImageRender = lazyLoadImageRender; + /** Markdown 文本转换为 HTML,该方法需使用[异步编程](https://ld246.com/article/1546828434083?r=Vaness) */ + public static md2html = md2html; + /** 页面 Markdown 文章渲染 */ + public static preview = previewRender; + /** 设置代码主题 */ + public static setCodeTheme = setCodeTheme; + /** 设置内容主题 */ + public static setContentTheme = setContentTheme; +} + +export default Vditor; diff --git a/src/libs/vditor/src/ts/constants.ts b/src/libs/vditor/src/ts/constants.ts new file mode 100644 index 00000000..5834dd41 --- /dev/null +++ b/src/libs/vditor/src/ts/constants.ts @@ -0,0 +1,178 @@ +const VDITOR_VERSION: string = '3.8.6'; + +const _VDITOR_VERSION = VDITOR_VERSION; + +export { _VDITOR_VERSION as VDITOR_VERSION }; + +export abstract class Constants { + public static readonly ZWSP: string = '\u200b'; + public static readonly DROP_EDITOR: string = 'application/editor'; + public static readonly MOBILE_WIDTH: number = 520; + public static readonly CLASS_MENU_DISABLED: string = 'vditor-menu--disabled'; + public static readonly EDIT_TOOLBARS: string[] = [ + 'emoji', + 'headings', + 'bold', + 'italic', + 'strike', + 'link', + 'list', + 'ordered-list', + 'outdent', + 'indent', + 'check', + 'line', + 'quote', + 'code', + 'inline-code', + 'insert-after', + 'insert-before', + 'upload', + 'record', + 'table' + ]; + public static readonly CODE_THEME: string[] = [ + 'abap', + 'algol', + 'algol_nu', + 'arduino', + 'autumn', + 'borland', + 'bw', + 'colorful', + 'dracula', + 'emacs', + 'friendly', + 'fruity', + 'github', + 'igor', + 'lovelace', + 'manni', + 'monokai', + 'monokailight', + 'murphy', + 'native', + 'paraiso-dark', + 'paraiso-light', + 'pastie', + 'perldoc', + 'pygments', + 'rainbow_dash', + 'rrt', + 'solarized-dark', + 'solarized-dark256', + 'solarized-light', + 'swapoff', + 'tango', + 'trac', + 'vim', + 'vs', + 'xcode', + 'ant-design' + ]; + public static readonly CODE_LANGUAGES: string[] = [ + 'mermaid', + 'echarts', + 'mindmap', + 'plantuml', + 'abc', + 'graphviz', + 'flowchart', + 'apache', + 'js', + 'ts', + 'html', + // common + 'properties', + 'apache', + 'bash', + 'c', + 'csharp', + 'cpp', + 'css', + 'coffeescript', + 'diff', + 'go', + 'xml', + 'http', + 'json', + 'java', + 'javascript', + 'kotlin', + 'less', + 'lua', + 'makefile', + 'markdown', + 'nginx', + 'objectivec', + 'php', + 'php-template', + 'perl', + 'plaintext', + 'python', + 'python-repl', + 'r', + 'ruby', + 'rust', + 'scss', + 'sql', + 'shell', + 'swift', + 'ini', + 'typescript', + 'vbnet', + 'yaml', + 'ada', + 'clojure', + 'dart', + 'erb', + 'fortran', + 'gradle', + 'haskell', + 'julia', + 'julia-repl', + 'lisp', + 'matlab', + 'pgsql', + 'powershell', + 'sql_more', + 'stata', + 'cmake', + 'mathematica' + ]; + public static readonly CDN = `https://cdn.jsdelivr.net/npm/vditor@${VDITOR_VERSION}`; + public static readonly MARKDOWN_OPTIONS = { + autoSpace: false, + codeBlockPreview: true, + fixTermTypo: false, + footnotes: true, + linkBase: '', + linkPrefix: '', + listStyle: false, + mark: false, + mathBlockPreview: true, + paragraphBeginningSpace: false, + sanitize: true, + toc: false + }; + public static readonly HLJS_OPTIONS = { + enable: true, + lineNumber: false, + style: 'github' + }; + public static readonly MATH_OPTIONS: IMath = { + engine: 'KaTeX', + inlineDigit: false, + macros: {} + }; + public static readonly THEME_OPTIONS = { + current: 'light', + list: { + 'ant-design': 'Ant Design', + dark: 'Dark', + light: 'Light', + wechat: 'WeChat' + }, + path: `${Constants.CDN}/dist/css/content-theme` + }; +} diff --git a/src/libs/vditor/src/ts/devtools/index.ts b/src/libs/vditor/src/ts/devtools/index.ts new file mode 100644 index 00000000..4f62a808 --- /dev/null +++ b/src/libs/vditor/src/ts/devtools/index.ts @@ -0,0 +1,83 @@ +import {getMarkdown} from "../markdown/getMarkdown"; +import {addScript} from "../util/addScript"; + +declare const echarts: { + init(element: HTMLElement): IEChart; +}; + +export class DevTools { + public element: HTMLDivElement; + private ASTChart: IEChart; + + constructor() { + this.element = document.createElement("div"); + this.element.className = "vditor-devtools"; + this.element.innerHTML = '
'; + } + + public renderEchart(vditor: IVditor) { + if (vditor.devtools.element.style.display !== "block") { + return; + } + + addScript(`${vditor.options.cdn}/dist/js/echarts/echarts.min.js`, "vditorEchartsScript").then(() => { + if (!this.ASTChart) { + this.ASTChart = echarts.init(vditor.devtools.element.lastElementChild as HTMLDivElement); + } + try { + (this.element.lastElementChild as HTMLElement).style.display = "block"; + this.element.firstElementChild.innerHTML = ""; + this.ASTChart.setOption({ + series: [ + { + data: JSON.parse(vditor.lute.RenderEChartsJSON(getMarkdown(vditor))), + initialTreeDepth: -1, + label: { + align: "left", + backgroundColor: "rgba(68, 77, 86, .68)", + borderRadius: 3, + color: "#d1d5da", + fontSize: 12, + lineHeight: 12, + offset: [9, 12], + padding: [2, 4, 2, 4], + position: "top", + verticalAlign: "middle", + }, + lineStyle: { + color: "#4285f4", + type: "curve", + width: 1, + }, + orient: "vertical", + roam: true, + type: "tree", + }, + ], + toolbox: { + bottom: 25, + emphasis: { + iconStyle: { + color: "#4285f4", + }, + }, + feature: { + restore: { + show: true, + }, + saveAsImage: { + show: true, + }, + }, + right: 15, + show: true, + }, + }); + this.ASTChart.resize(); + } catch (e) { + (this.element.lastElementChild as HTMLElement).style.display = "none"; + this.element.firstElementChild.innerHTML = e; + } + }); + } +} diff --git a/src/libs/vditor/src/ts/export/index.ts b/src/libs/vditor/src/ts/export/index.ts new file mode 100644 index 00000000..b71861b3 --- /dev/null +++ b/src/libs/vditor/src/ts/export/index.ts @@ -0,0 +1,81 @@ +import {getHTML} from "../markdown/getHTML"; +import {getMarkdown} from "../markdown/getMarkdown"; + +export const download = (vditor: IVditor, content: string, filename: string) => { + const aElement = document.createElement("a"); + if ("download" in aElement) { + aElement.download = filename; + aElement.style.display = "none"; + aElement.href = URL.createObjectURL(new Blob([content])); + + document.body.appendChild(aElement); + aElement.click(); + aElement.remove(); + } else { + vditor.tip.show(window.VditorI18n.downloadTip, 0); + } +}; + +export const exportMarkdown = (vditor: IVditor) => { + const content = getMarkdown(vditor); + download(vditor, content, content.substr(0, 10) + ".md"); +}; + +export const exportPDF = (vditor: IVditor) => { + vditor.tip.show(window.VditorI18n.generate, 3800); + const iframe = document.querySelector("iframe"); + iframe.contentDocument.open(); + iframe.contentDocument.write(` + + +`); + iframe.contentDocument.close(); + setTimeout(() => { + iframe.contentWindow.postMessage(getMarkdown(vditor), "*"); + }, 200); +}; + +export const exportHTML = (vditor: IVditor) => { + const content = getHTML(vditor); + const html = ` + + +${Constants.ZWSP}
${quoteElement.innerHTML}
` : quoteElement.innerHTML; + } + } else if (commandName === "link") { + const aElement = hasClosestByAttribute(range.startContainer, "data-type", "a") as HTMLElement; + if (aElement) { + const aTextElement = hasClosestByClassName(range.startContainer, "vditor-ir__link"); + if (aTextElement) { + range.insertNode(document.createElement("wbr")); + aElement.outerHTML = aTextElement.innerHTML; + } else { + aElement.outerHTML = aElement.querySelector(".vditor-ir__link").innerHTML + "${blockElement.outerHTML}`; + useHighlight = false; + actionBtn.classList.add("vditor-menu--current"); + } + } else if (commandName === "link") { + let html; + if (range.toString() === "") { + html = `${prefix}
[ToC]
" + tocHTML); + } else if (vditor.currentMode === "ir" && !vditor.preview.element.contains(contentElement)) { + tempElement.innerHTML = vditor.lute.SpinVditorIRDOM("[ToC]
" + tocHTML); + } else { + tempElement.innerHTML = vditor.lute.HTML2VditorDOM("[ToC]
" + tocHTML); + } + vditor.lute.SetToC(vditor.options.preview.markdown.toc); + } else { + targetElement.classList.add("vditor-outline"); + const lute = Lute.New(); + lute.SetToC(true); + tempElement.innerHTML = lute.HTML2VditorDOM("[ToC]
" + tocHTML); + } + const headingsElement = tempElement.firstElementChild.querySelectorAll("li > span[data-target-id]"); + headingsElement.forEach((item, index) => { + if (item.nextElementSibling && item.nextElementSibling.tagName === "UL") { + item.innerHTML = `${item.innerHTML}`; + } else { + item.innerHTML = `${item.innerHTML}`; + } + item.setAttribute("data-target-id", ids[index]); + }); + tocHTML = tempElement.firstElementChild.innerHTML; + if (headingsElement.length === 0) { + targetElement.innerHTML = ""; + return tocHTML; + } + targetElement.innerHTML = tocHTML; + if (vditor) { + mathRender(targetElement as HTMLElement, { + cdn: vditor.options.cdn, + math: vditor.options.preview.math, + }); + } + targetElement.firstElementChild.addEventListener("click", (event: Event) => { + let target = event.target as HTMLElement; + while (target && !target.isEqualNode(targetElement)) { + if (target.classList.contains("vditor-outline__action")) { + if (target.classList.contains("vditor-outline__action--close")) { + target.classList.remove("vditor-outline__action--close"); + target.parentElement.nextElementSibling.setAttribute("style", "display:block"); + } else { + target.classList.add("vditor-outline__action--close"); + target.parentElement.nextElementSibling.setAttribute("style", "display:none"); + } + event.preventDefault(); + event.stopPropagation(); + break; + } else if (target.getAttribute("data-target-id")) { + event.preventDefault(); + event.stopPropagation(); + const idElement = document.getElementById(target.getAttribute("data-target-id")); + if (!idElement) { + return; + } + if (vditor) { + if (vditor.options.height === "auto") { + let windowScrollY = idElement.offsetTop + vditor.element.offsetTop; + if (!vditor.options.toolbarConfig.pin) { + windowScrollY += vditor.toolbar.element.offsetHeight; + } + window.scrollTo(window.scrollX, windowScrollY); + } else { + if (vditor.element.offsetTop < window.scrollY) { + window.scrollTo(window.scrollX, vditor.element.offsetTop); + } + if (vditor.preview.element.contains(contentElement)) { + contentElement.parentElement.scrollTop = idElement.offsetTop; + } else { + contentElement.scrollTop = idElement.offsetTop; + } + } + } else { + window.scrollTo(window.scrollX, idElement.offsetTop); + } + break; + } + target = target.parentElement; + } + }); + return tocHTML; +}; diff --git a/src/libs/vditor/src/ts/markdown/plantumlRender.ts b/src/libs/vditor/src/ts/markdown/plantumlRender.ts new file mode 100644 index 00000000..5e39f66b --- /dev/null +++ b/src/libs/vditor/src/ts/markdown/plantumlRender.ts @@ -0,0 +1,32 @@ +import {Constants} from "../constants"; +import {addScript} from "../util/addScript"; +import {plantumlRenderAdapter} from "./adapterRender"; + +declare const plantumlEncoder: { + encode(options: string): string, +}; + +export const plantumlRender = (element: (HTMLElement | Document) = document, cdn = Constants.CDN) => { + const plantumlElements = plantumlRenderAdapter.getElements(element); + if (plantumlElements.length === 0) { + return; + } + addScript(`${cdn}/dist/js/plantuml/plantuml-encoder.min.js`, "vditorPlantumlScript").then(() => { + plantumlElements.forEach((e: HTMLDivElement) => { + if (e.parentElement.classList.contains("vditor-wysiwyg__pre") || + e.parentElement.classList.contains("vditor-ir__marker--pre")) { + return; + } + const text = plantumlRenderAdapter.getCode(e).trim(); + if (!text) { + return; + } + try { + e.innerHTML = `+ 下一代的 Markdown 编辑器,为未来而构建 +
+
+ <audio controls="controls" src="${path}"></audio>`;
+ } else if (vditor.currentMode === "ir") {
+ succFileText += `\n`;
+ } else {
+ succFileText += `[${filename}](${path})\n`;
+ }
+ } else if (type.indexOf(".apng") === 0
+ || type.indexOf(".bmp") === 0
+ || type.indexOf(".gif") === 0
+ || type.indexOf(".ico") === 0 || type.indexOf(".cur") === 0
+ || type.indexOf(".jpg") === 0 || type.indexOf(".jpeg") === 0 || type.indexOf(".jfif") === 0 || type.indexOf(".pjp") === 0 || type.indexOf(".pjpeg") === 0
+ || type.indexOf(".png") === 0
+ || type.indexOf(".svg") === 0
+ || type.indexOf(".webp") === 0) {
+ if (vditor.currentMode === "wysiwyg") {
+ succFileText += `${Constants.ZWSP}
${Constants.ZWSP}
${Constants.ZWSP}
${itemElement.parentElement.children[i].innerHTML.trimLeft()}
`; + } + itemElement.parentElement.insertAdjacentHTML("beforebegin", pHTML); + itemElement.parentElement.remove(); + } else { + if (!itemElement) { + // 添加 + let blockElement = hasClosestByAttribute(range.startContainer, "data-block", "0"); + if (!blockElement) { + vditor[vditor.currentMode].element.querySelector("wbr").remove(); + blockElement = vditor[vditor.currentMode].element.querySelector("p"); + blockElement.innerHTML = "${innerHTMLList.join("\n")}
`; + } + + pElement.insertAdjacentHTML("afterend", + `${pInnerHTML}${Constants.ZWSP}
${blockElement.outerHTML}`; + setRangeByWbr(vditor.wysiwyg.element, range); + afterRenderEvent(vditor); + event.preventDefault(); + return true; + } + + if (insertAfterBlock(vditor, event, range, blockquoteElement, blockquoteElement)) { + return true; + } + if (insertBeforeBlock(vditor, event, range, blockquoteElement, blockquoteElement)) { + return true; + } + } + return false; +}; + +export const fixTask = (vditor: IVditor, range: Range, event: KeyboardEvent) => { + const startContainer = range.startContainer; + const taskItemElement = hasClosestByClassName(startContainer, "vditor-task"); + if (taskItemElement) { + if (matchHotKey("⇧⌘J", event)) { + // ctrl + shift: toggle checked + const inputElement = taskItemElement.firstElementChild as HTMLInputElement; + if (inputElement.checked) { + inputElement.removeAttribute("checked"); + } else { + inputElement.setAttribute("checked", "checked"); + } + execAfterRender(vditor); + event.preventDefault(); + return true; + } + + // Backspace: 在选择框前进行删除 + if (event.key === "Backspace" && !isCtrl(event) && !event.shiftKey && !event.altKey && range.toString() === "" + && range.startOffset === 1 + && ((startContainer.nodeType === 3 && startContainer.previousSibling && + (startContainer.previousSibling as HTMLElement).tagName === "INPUT") + || startContainer.nodeType !== 3)) { + const previousElement = taskItemElement.previousElementSibling; + taskItemElement.querySelector("input").remove(); + if (previousElement) { + const lastNode = getLastNode(previousElement); + lastNode.parentElement.insertAdjacentHTML("beforeend", "
\n
') === 0) { + // Xcode + isCode = true; + } + if (tempElement.childElementCount === 1 && tempElement.firstElementChild.tagName === "TABLE" && + tempElement.querySelector(".line-number") && tempElement.querySelector(".line-content")) { + // 网页源码 + isCode = true; + } + + if (isCode) { + const code = text || html; + if (/\n/.test(code) || pres.length === 1) { + if (type === "wysiwyg") { + return `
${
+ code.replace(/&/g, "&").replace(/ 获取位置信息
+ const parent = range.startContainer.parentElement;
+ if (parent && parent.getClientRects().length > 0) {
+ cursorRect = parent.getClientRects()[0];
+ } else {
+ return {
+ left: 0,
+ top: 0,
+ };
+ }
+ } else {
+ const children = (range.startContainer as Element).children;
+ if (children[range.startOffset] &&
+ children[range.startOffset].getClientRects().length > 0) {
+ // markdown 模式回车
+ cursorRect = children[range.startOffset].getClientRects()[0];
+ } else if (range.startContainer.childNodes.length > 0) {
+ // in table or code block
+ const cloneRange = range.cloneRange();
+ range.selectNode(range.startContainer.childNodes[Math.max(0, range.startOffset - 1)]);
+ cursorRect = range.getClientRects()[0];
+ range.setEnd(cloneRange.endContainer, cloneRange.endOffset);
+ range.setStart(cloneRange.startContainer, cloneRange.startOffset);
+ } else {
+ cursorRect = (range.startContainer as HTMLElement).getClientRects()[0];
+ }
+ if (!cursorRect) {
+ let parentElement = range.startContainer.childNodes[range.startOffset] as HTMLElement;
+ while (!parentElement.getClientRects ||
+ (parentElement.getClientRects && parentElement.getClientRects().length === 0)) {
+ parentElement = parentElement.parentElement;
+ }
+ cursorRect = parentElement.getClientRects()[0];
+ }
+ }
+
+ } else {
+ cursorRect = range.getClientRects()[0];
+ }
+
+ return {
+ left: cursorRect.left - parentRect.left,
+ top: cursorRect.top - parentRect.top,
+ };
+};
+
+export const selectIsEditor = (editor: HTMLElement, range?: Range) => {
+ if (!range) {
+ if (getSelection().rangeCount === 0) {
+ return false;
+ } else {
+ range = getSelection().getRangeAt(0);
+ }
+ }
+ const container = range.commonAncestorContainer;
+
+ return editor.isEqualNode(container) || editor.contains(container);
+};
+
+export const setSelectionFocus = (range: Range) => {
+ const selection = window.getSelection();
+ selection.removeAllRanges();
+ selection.addRange(range);
+};
+
+export const getSelectPosition = (selectElement: HTMLElement, editorElement: HTMLElement, range?: Range) => {
+ const position = {
+ end: 0,
+ start: 0,
+ };
+
+ if (!range) {
+ if (getSelection().rangeCount === 0) {
+ return position;
+ }
+ range = window.getSelection().getRangeAt(0);
+ }
+
+ if (selectIsEditor(editorElement, range)) {
+ const preSelectionRange = range.cloneRange();
+ if (selectElement.childNodes[0] && selectElement.childNodes[0].childNodes[0]) {
+ preSelectionRange.setStart(selectElement.childNodes[0].childNodes[0], 0);
+ } else {
+ preSelectionRange.selectNodeContents(selectElement);
+ }
+ preSelectionRange.setEnd(range.startContainer, range.startOffset);
+ position.start = preSelectionRange.toString().length;
+ position.end = position.start + range.toString().length;
+ }
+ return position;
+};
+
+export const setSelectionByPosition = (start: number, end: number, editor: HTMLElement) => {
+ let charIndex = 0;
+ let line = 0;
+ let pNode = editor.childNodes[line];
+ let foundStart = false;
+ let stop = false;
+ start = Math.max(0, start);
+ end = Math.max(0, end);
+
+ const range = editor.ownerDocument.createRange();
+ range.setStart(pNode || editor, 0);
+ range.collapse(true);
+
+ while (!stop && pNode) {
+ const nextCharIndex = charIndex + pNode.textContent.length;
+ if (!foundStart && start >= charIndex && start <= nextCharIndex) {
+ if (start === 0) {
+ range.setStart(pNode, 0);
+ } else {
+ if (pNode.childNodes[0].nodeType === 3) {
+ range.setStart(pNode.childNodes[0], start - charIndex);
+ } else if (pNode.nextSibling) {
+ range.setStartBefore(pNode.nextSibling);
+ } else {
+ range.setStartAfter(pNode);
+ }
+ }
+ foundStart = true;
+ if (start === end) {
+ stop = true;
+ break;
+ }
+ }
+ if (foundStart && end >= charIndex && end <= nextCharIndex) {
+ if (end === 0) {
+ range.setEnd(pNode, 0);
+ } else {
+ if (pNode.childNodes[0].nodeType === 3) {
+ range.setEnd(pNode.childNodes[0], end - charIndex);
+ } else if (pNode.nextSibling) {
+ range.setEndBefore(pNode.nextSibling);
+ } else {
+ range.setEndAfter(pNode);
+ }
+ }
+ stop = true;
+ }
+ charIndex = nextCharIndex;
+ pNode = editor.childNodes[++line];
+ }
+
+ if (!stop && editor.childNodes[line - 1]) {
+ range.setStartBefore(editor.childNodes[line - 1]);
+ }
+
+ setSelectionFocus(range);
+ return range;
+};
+
+export const setRangeByWbr = (element: HTMLElement, range: Range) => {
+ const wbrElement = element.querySelector("wbr");
+ if (!wbrElement) {
+ return;
+ }
+ if (!wbrElement.previousElementSibling) {
+ if (wbrElement.previousSibling) {
+ // text ${Constants.ZWSP} ${node.innerHTML} ${node.outerHTML} ${node.outerHTML} ``` ``` ${blockElement.innerHTML} ${quoteElement.innerHTML}
https://github.com/Vanessa219/vditor/issues/400
+ range.setStartBefore(wbrElement.nextSibling);
+ }
+ } else {
+ // 内容为空
+ range.setStart(wbrElement.parentElement, 0);
+ }
+ } else {
+ if (wbrElement.previousElementSibling.isSameNode(wbrElement.previousSibling)) {
+ if (wbrElement.previousElementSibling.lastChild) {
+ // text ");
+ } else {
+ tableElement.rows[i].lastElementChild.insertAdjacentHTML("afterend", " ");
+ }
+ }
+ } else {
+ for (let k = oldColumn - 1; k >= column; k--) {
+ tableElement.rows[i].cells[k].remove();
+ }
+ }
+ }
+ }
+
+ if (oldRow !== row) {
+ const rowDiff = row - oldRow;
+ if (rowDiff > 0) {
+ let rowHTML = "";
+ for (let m = 0; m < column; m++) {
+ rowHTML += " ");
+ }
+ }
+ } else {
+ for (let m = oldRow - 1; m >= row; m--) {
+ tableElement.rows[m].remove();
+ if (tableElement.rows.length === 1) {
+ tableElement.querySelector("tbody").remove();
+ }
+ }
+ }
+ }
+ };
+
+ const setAlign = (type: string) => {
+ setTableAlign(tableElement, type);
+ if (type === "right") {
+ left.classList.remove("vditor-icon--current");
+ center.classList.remove("vditor-icon--current");
+ right.classList.add("vditor-icon--current");
+ } else if (type === "center") {
+ left.classList.remove("vditor-icon--current");
+ right.classList.remove("vditor-icon--current");
+ center.classList.add("vditor-icon--current");
+ } else {
+ center.classList.remove("vditor-icon--current");
+ right.classList.remove("vditor-icon--current");
+ left.classList.add("vditor-icon--current");
+ }
+ setSelectionFocus(range);
+ afterRenderEvent(vditor);
+ };
+
+ const td = hasClosestByMatchTag(typeElement, "TD");
+ const th = hasClosestByMatchTag(typeElement, "TH");
+ let alignType = "left";
+ if (td) {
+ alignType = td.getAttribute("align") || "left";
+ } else if (th) {
+ alignType = th.getAttribute("align") || "center";
+ }
+
+ const left = document.createElement("button");
+ left.setAttribute("type", "button");
+ left.setAttribute("aria-label", window.VditorI18n.alignLeft + "<" + updateHotkeyTip("⇧⌘L") + ">");
+ left.setAttribute("data-type", "left");
+ left.innerHTML =
+ '';
+ left.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n" +
+ (alignType === "left" ? " vditor-icon--current" : "");
+ left.onclick = () => {
+ setAlign("left");
+ };
+
+ const center = document.createElement("button");
+ center.setAttribute("type", "button");
+ center.setAttribute("aria-label", window.VditorI18n.alignCenter + "<" + updateHotkeyTip("⇧⌘C") + ">");
+ center.setAttribute("data-type", "center");
+ center.innerHTML =
+ '';
+ center.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n" +
+ (alignType === "center" ? " vditor-icon--current" : "");
+ center.onclick = () => {
+ setAlign("center");
+ };
+
+ const right = document.createElement("button");
+ right.setAttribute("type", "button");
+ right.setAttribute("aria-label", window.VditorI18n.alignRight + "<" + updateHotkeyTip("⇧⌘R") + ">");
+ right.setAttribute("data-type", "right");
+ right.innerHTML =
+ '';
+ right.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n" +
+ (alignType === "right" ? " vditor-icon--current" : "");
+ right.onclick = () => {
+ setAlign("right");
+ };
+
+ const insertRowElement = document.createElement("button");
+ insertRowElement.setAttribute("type", "button");
+ insertRowElement.setAttribute("aria-label", window.VditorI18n.insertRowBelow + "<" + updateHotkeyTip("⌘=") + ">");
+ insertRowElement.setAttribute("data-type", "insertRow");
+ insertRowElement.innerHTML =
+ '';
+ insertRowElement.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n";
+ insertRowElement.onclick = () => {
+ const startContainer = getSelection().getRangeAt(0)
+ .startContainer;
+ const cellElement =
+ hasClosestByMatchTag(startContainer, "TD") ||
+ hasClosestByMatchTag(startContainer, "TH");
+ if (cellElement) {
+ insertRow(vditor, range, cellElement);
+ }
+ };
+
+ const insertRowBElement = document.createElement("button");
+ insertRowBElement.setAttribute("type", "button");
+ insertRowBElement.setAttribute("aria-label",
+ window.VditorI18n.insertRowAbove + "<" + updateHotkeyTip("⇧⌘F") + ">");
+ insertRowBElement.setAttribute("data-type", "insertRow");
+ insertRowBElement.innerHTML =
+ '';
+ insertRowBElement.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n";
+ insertRowBElement.onclick = () => {
+ const startContainer = getSelection().getRangeAt(0)
+ .startContainer;
+ const cellElement =
+ hasClosestByMatchTag(startContainer, "TD") ||
+ hasClosestByMatchTag(startContainer, "TH");
+ if (cellElement) {
+ insertRowAbove(vditor, range, cellElement);
+ }
+ };
+
+ const insertColumnElement = document.createElement("button");
+ insertColumnElement.setAttribute("type", "button");
+ insertColumnElement.setAttribute("aria-label", window.VditorI18n.insertColumnRight + "<" + updateHotkeyTip("⇧⌘=") + ">");
+ insertColumnElement.setAttribute("data-type", "insertColumn");
+ insertColumnElement.innerHTML =
+ '';
+ insertColumnElement.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n";
+ insertColumnElement.onclick = () => {
+ const startContainer = getSelection().getRangeAt(0)
+ .startContainer;
+ const cellElement =
+ hasClosestByMatchTag(startContainer, "TD") ||
+ hasClosestByMatchTag(startContainer, "TH");
+ if (cellElement) {
+ insertColumn(vditor, tableElement, cellElement);
+ }
+ };
+
+ const insertColumnBElement = document.createElement("button");
+ insertColumnBElement.setAttribute("type", "button");
+ insertColumnBElement.setAttribute("aria-label", window.VditorI18n.insertColumnLeft + "<" + updateHotkeyTip("⇧⌘G") + ">");
+ insertColumnBElement.setAttribute("data-type", "insertColumn");
+ insertColumnBElement.innerHTML =
+ '';
+ insertColumnBElement.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n";
+ insertColumnBElement.onclick = () => {
+ const startContainer = getSelection().getRangeAt(0)
+ .startContainer;
+ const cellElement =
+ hasClosestByMatchTag(startContainer, "TD") ||
+ hasClosestByMatchTag(startContainer, "TH");
+ if (cellElement) {
+ insertColumn(vditor, tableElement, cellElement, "beforebegin");
+ }
+ };
+
+ const deleteRowElement = document.createElement("button");
+ deleteRowElement.setAttribute("type", "button");
+ deleteRowElement.setAttribute("aria-label", window.VditorI18n["delete-row"] + "<" + updateHotkeyTip("⌘-") + ">");
+ deleteRowElement.setAttribute("data-type", "deleteRow");
+ deleteRowElement.innerHTML =
+ '';
+ deleteRowElement.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n";
+ deleteRowElement.onclick = () => {
+ const startContainer = getSelection().getRangeAt(0)
+ .startContainer;
+ const cellElement =
+ hasClosestByMatchTag(startContainer, "TD") ||
+ hasClosestByMatchTag(startContainer, "TH");
+ if (cellElement) {
+ deleteRow(vditor, range, cellElement);
+ }
+ };
+
+ const deleteColumnElement = document.createElement("button");
+ deleteColumnElement.setAttribute("type", "button");
+ deleteColumnElement.setAttribute("aria-label", window.VditorI18n["delete-column"] + "<" + updateHotkeyTip("⇧⌘-") + ">");
+ deleteColumnElement.setAttribute("data-type", "deleteColumn");
+ deleteColumnElement.innerHTML =
+ '';
+ deleteColumnElement.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n";
+ deleteColumnElement.onclick = () => {
+ const startContainer = getSelection().getRangeAt(0)
+ .startContainer;
+ const cellElement =
+ hasClosestByMatchTag(startContainer, "TD") ||
+ hasClosestByMatchTag(startContainer, "TH");
+ if (cellElement) {
+ deleteColumn(vditor, range, tableElement, cellElement);
+ }
+ };
+
+ const inputWrap = document.createElement("span");
+ inputWrap.setAttribute("aria-label", window.VditorI18n.row);
+ inputWrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const input = document.createElement("input");
+ inputWrap.appendChild(input);
+ input.type = "number";
+ input.min = "1";
+ input.className = "vditor-input";
+ input.style.width = "42px";
+ input.style.textAlign = "center";
+ input.setAttribute("placeholder", window.VditorI18n.row);
+ input.value = tableElement.rows.length.toString();
+ input.oninput = () => {
+ updateTable();
+ };
+ input.onkeydown = (event) => {
+ if (event.isComposing) {
+ return;
+ }
+ if (event.key === "Tab") {
+ input2.focus();
+ input2.select();
+ event.preventDefault();
+ return;
+ }
+ removeBlockElement(vditor, event);
+ };
+
+ const input2Wrap = document.createElement("span");
+ input2Wrap.setAttribute("aria-label", window.VditorI18n.column);
+ input2Wrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const input2 = document.createElement("input");
+ input2Wrap.appendChild(input2);
+ input2.type = "number";
+ input2.min = "1";
+ input2.className = "vditor-input";
+ input2.style.width = "42px";
+ input2.style.textAlign = "center";
+ input2.setAttribute("placeholder", window.VditorI18n.column);
+ input2.value = tableElement.rows[0].cells.length.toString();
+ input2.oninput = () => {
+ updateTable();
+ };
+ input2.onkeydown = (event) => {
+ if (event.isComposing) {
+ return;
+ }
+ if (event.key === "Tab") {
+ input.focus();
+ input.select();
+ event.preventDefault();
+ return;
+ }
+ removeBlockElement(vditor, event);
+ };
+
+ genUp(range, tableElement, vditor);
+ genDown(range, tableElement, vditor);
+ genClose(tableElement, vditor);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", left);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", center);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", right);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", insertRowBElement);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", insertRowElement);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", insertColumnBElement);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", insertColumnElement);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", deleteRowElement);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", deleteColumnElement);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", inputWrap);
+ vditor.wysiwyg.popover.insertAdjacentHTML("beforeend", " x ");
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", input2Wrap);
+ setPopoverPosition(vditor, tableElement);
+ }
+
+ // link ref popover
+ const linkRefElement = hasClosestByAttribute(typeElement, "data-type", "link-ref");
+ if (linkRefElement) {
+ genLinkRefPopover(vditor, linkRefElement);
+ }
+
+ // footnote popover
+ const footnotesRefElement = hasClosestByAttribute(typeElement, "data-type", "footnotes-ref");
+ if (footnotesRefElement) {
+ const lang: keyof II18n | "" = vditor.options.lang;
+ const options: IOptions = vditor.options;
+ vditor.wysiwyg.popover.innerHTML = "";
+
+ const inputWrap = document.createElement("span");
+ inputWrap.setAttribute("aria-label", window.VditorI18n.footnoteRef + "<" + updateHotkeyTip("⌥Enter") + ">");
+ inputWrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const input = document.createElement("input");
+ inputWrap.appendChild(input);
+ input.className = "vditor-input";
+ input.setAttribute("placeholder", window.VditorI18n.footnoteRef + "<" + updateHotkeyTip("⌥Enter") + ">");
+ input.style.width = "120px";
+ input.value = footnotesRefElement.getAttribute("data-footnotes-label");
+ input.oninput = () => {
+ if (input.value.trim() !== "") {
+ footnotesRefElement.setAttribute("data-footnotes-label", input.value);
+ }
+ };
+ input.onkeydown = (event) => {
+ if (event.isComposing) {
+ return;
+ }
+ if (
+ !isCtrl(event) &&
+ !event.shiftKey &&
+ event.altKey &&
+ event.key === "Enter"
+ ) {
+ range.selectNodeContents(footnotesRefElement);
+ range.collapse(false);
+ setSelectionFocus(range);
+ event.preventDefault();
+ return;
+ }
+ removeBlockElement(vditor, event);
+ };
+
+ genClose(footnotesRefElement, vditor);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", inputWrap);
+ setPopoverPosition(vditor, footnotesRefElement);
+ }
+
+ let blockRenderElement = hasClosestByClassName(typeElement, "vditor-wysiwyg__block") as HTMLElement;
+ // block popover: math-inline, math-block, html-block, html-inline, code-block, html-entity
+ if (
+ blockRenderElement &&
+ blockRenderElement.getAttribute("data-type").indexOf("block") > -1
+ ) {
+ const lang: keyof II18n | "" = vditor.options.lang;
+ const options: IOptions = vditor.options;
+ vditor.wysiwyg.popover.innerHTML = "";
+ genUp(range, blockRenderElement, vditor);
+ genDown(range, blockRenderElement, vditor);
+ genClose(blockRenderElement, vditor);
+
+ if (blockRenderElement.getAttribute("data-type") === "code-block") {
+ const languageWrap = document.createElement("span");
+ languageWrap.setAttribute("aria-label", window.VditorI18n.language + "<" + updateHotkeyTip("⌥Enter") + ">");
+ languageWrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const language = document.createElement("input");
+ languageWrap.appendChild(language);
+
+ const codeElement =
+ blockRenderElement.firstElementChild.firstElementChild;
+
+ language.className = "vditor-input";
+ language.setAttribute("placeholder",
+ window.VditorI18n.language + "<" + updateHotkeyTip("⌥Enter") + ">");
+ language.value =
+ codeElement.className.indexOf("language-") > -1
+ ? codeElement.className.split("-")[1].split(" ")[0]
+ : "";
+ language.oninput = () => {
+ if (language.value.trim() !== "") {
+ codeElement.className = `language-${language.value}`;
+ } else {
+ codeElement.className = "";
+ vditor.hint.recentLanguage = "";
+ }
+ if (blockRenderElement.lastElementChild.classList.contains("vditor-wysiwyg__preview")) {
+ blockRenderElement.lastElementChild.innerHTML =
+ blockRenderElement.firstElementChild.innerHTML;
+ processCodeRender(blockRenderElement.lastElementChild as HTMLElement, vditor);
+ }
+ afterRenderEvent(vditor);
+ };
+ language.onkeydown = (event: KeyboardEvent) => {
+ if (event.isComposing) {
+ return;
+ }
+ if (removeBlockElement(vditor, event)) {
+ return;
+ }
+ if (
+ event.key === "Escape" &&
+ vditor.hint.element.style.display === "block"
+ ) {
+ vditor.hint.element.style.display = "none";
+ event.preventDefault();
+ return;
+ }
+ if (
+ !isCtrl(event) &&
+ !event.shiftKey &&
+ event.altKey &&
+ event.key === "Enter"
+ ) {
+ range.setStart(codeElement.firstChild, 0);
+ range.collapse(true);
+ setSelectionFocus(range);
+ }
+ vditor.hint.select(event, vditor);
+ };
+ language.onkeyup = (event: KeyboardEvent) => {
+ if (
+ event.isComposing ||
+ event.key === "Enter" ||
+ event.key === "ArrowUp" ||
+ event.key === "Escape" ||
+ event.key === "ArrowDown"
+ ) {
+ return;
+ }
+ const matchLangData: IHintData[] = [];
+ const key = language.value.substring(0, language.selectionStart);
+ Constants.CODE_LANGUAGES.forEach((keyName) => {
+ if (keyName.indexOf(key.toLowerCase()) > -1) {
+ matchLangData.push({
+ html: keyName,
+ value: keyName,
+ });
+ }
+ });
+ vditor.hint.genHTML(matchLangData, key, vditor);
+ event.preventDefault();
+ };
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", languageWrap);
+ }
+ setPopoverPosition(vditor, blockRenderElement);
+ } else {
+ if (!blockRenderElement) {
+ vditor.wysiwyg.element
+ .querySelectorAll(".vditor-wysiwyg__preview")
+ .forEach((itemElement) => {
+ const previousElement = itemElement.previousElementSibling as HTMLElement;
+ previousElement.style.display = "none";
+ });
+ }
+ blockRenderElement = undefined;
+ }
+
+ if (headingElement) {
+ vditor.wysiwyg.popover.innerHTML = "";
+
+ const inputWrap = document.createElement("span");
+ inputWrap.setAttribute("aria-label", "ID" + "<" + updateHotkeyTip("⌥Enter") + ">");
+ inputWrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const input = document.createElement("input");
+ inputWrap.appendChild(input);
+ input.className = "vditor-input";
+ input.setAttribute("placeholder", "ID" + "<" + updateHotkeyTip("⌥Enter") + ">");
+ input.style.width = "120px";
+ input.value = headingElement.getAttribute("data-id") || "";
+ input.oninput = () => {
+ headingElement.setAttribute("data-id", input.value);
+ };
+ input.onkeydown = (event) => {
+ if (event.isComposing) {
+ return;
+ }
+ if (
+ !isCtrl(event) &&
+ !event.shiftKey &&
+ event.altKey &&
+ event.key === "Enter"
+ ) {
+ range.selectNodeContents(headingElement);
+ range.collapse(false);
+ setSelectionFocus(range);
+ event.preventDefault();
+ return;
+ }
+ removeBlockElement(vditor, event);
+ };
+
+ genUp(range, headingElement, vditor);
+ genDown(range, headingElement, vditor);
+ genClose(headingElement, vditor);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", inputWrap);
+ setPopoverPosition(vditor, headingElement);
+ }
+
+ // a popover
+ if (aElement) {
+ genAPopover(vditor, aElement);
+ }
+
+ if (
+ !blockquoteElement &&
+ !liElement &&
+ !tableElement &&
+ !blockRenderElement &&
+ !aElement &&
+ !linkRefElement &&
+ !footnotesRefElement &&
+ !headingElement &&
+ !tocElement
+ ) {
+ const blockElement = hasClosestByAttribute(typeElement, "data-block", "0");
+ if (
+ blockElement &&
+ blockElement.parentElement.isEqualNode(vditor.wysiwyg.element)
+ ) {
+ vditor.wysiwyg.popover.innerHTML = "";
+ genUp(range, blockElement, vditor);
+ genDown(range, blockElement, vditor);
+ genClose(blockElement, vditor);
+
+ setPopoverPosition(vditor, blockElement);
+ } else {
+ vditor.wysiwyg.popover.style.display = "none";
+ }
+ }
+
+ // 反斜杠特殊处理
+ vditor.wysiwyg.element
+ .querySelectorAll('span[data-type="backslash"] > span')
+ .forEach((item: HTMLElement) => {
+ item.style.display = "none";
+ });
+ const backslashElement = hasClosestByAttribute(range.startContainer, "data-type", "backslash");
+ if (backslashElement) {
+ backslashElement.querySelector("span").style.display = "inline";
+ }
+ }, 200);
+};
+
+const setPopoverPosition = (vditor: IVditor, element: HTMLElement) => {
+ let targetElement = element;
+ const tableElement = hasClosestByMatchTag(element, "TABLE");
+ if (tableElement) {
+ targetElement = tableElement;
+ }
+ vditor.wysiwyg.popover.style.left = "0";
+ vditor.wysiwyg.popover.style.display = "block";
+ vditor.wysiwyg.popover.style.top =
+ Math.max(-8, targetElement.offsetTop - 21 - vditor.wysiwyg.element.scrollTop) + "px";
+ vditor.wysiwyg.popover.style.left =
+ Math.min(targetElement.offsetLeft, vditor.wysiwyg.element.clientWidth - vditor.wysiwyg.popover.clientWidth) + "px";
+ vditor.wysiwyg.popover.setAttribute("data-top", (targetElement.offsetTop - 21).toString());
+};
+
+export const genLinkRefPopover = (vditor: IVditor, linkRefElement: HTMLElement) => {
+ vditor.wysiwyg.popover.innerHTML = "";
+
+ const updateLinkRef = () => {
+ if (input.value.trim() !== "") {
+ if (linkRefElement.tagName === "IMG") {
+ linkRefElement.setAttribute("alt", input.value);
+ } else {
+ linkRefElement.textContent = input.value;
+ }
+ }
+ // data-link-label
+ if (input1.value.trim() !== "") {
+ linkRefElement.setAttribute("data-link-label", input1.value);
+ }
+ };
+
+ const inputWrap = document.createElement("span");
+ inputWrap.setAttribute("aria-label", window.VditorI18n.textIsNotEmpty);
+ inputWrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const input = document.createElement("input");
+ inputWrap.appendChild(input);
+ input.className = "vditor-input";
+ input.setAttribute("placeholder", window.VditorI18n.textIsNotEmpty);
+ input.style.width = "120px";
+ input.value =
+ linkRefElement.getAttribute("alt") || linkRefElement.textContent;
+ input.oninput = () => {
+ updateLinkRef();
+ };
+ input.onkeydown = (event) => {
+ if (removeBlockElement(vditor, event)) {
+ return;
+ }
+ linkHotkey(vditor, linkRefElement, event, input1);
+ };
+
+ const input1Wrap = document.createElement("span");
+ input1Wrap.setAttribute("aria-label", window.VditorI18n.linkRef);
+ input1Wrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const input1 = document.createElement("input");
+ input1Wrap.appendChild(input1);
+ input1.className = "vditor-input";
+ input1.setAttribute("placeholder", window.VditorI18n.linkRef);
+ input1.value = linkRefElement.getAttribute("data-link-label");
+ input1.oninput = () => {
+ updateLinkRef();
+ };
+ input1.onkeydown = (event) => {
+ if (removeBlockElement(vditor, event)) {
+ return;
+ }
+ linkHotkey(vditor, linkRefElement, event, input);
+ };
+
+ genClose(linkRefElement, vditor);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", inputWrap);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", input1Wrap);
+ setPopoverPosition(vditor, linkRefElement);
+};
+
+const genUp = (range: Range, element: HTMLElement, vditor: IVditor) => {
+ const previousElement = element.previousElementSibling;
+ if (
+ !previousElement ||
+ (!element.parentElement.isEqualNode(vditor.wysiwyg.element) &&
+ element.tagName !== "LI")
+ ) {
+ return;
+ }
+ const upElement = document.createElement("button");
+ upElement.setAttribute("type", "button");
+ upElement.setAttribute("data-type", "up");
+ upElement.setAttribute("aria-label", window.VditorI18n.up + "<" + updateHotkeyTip("⇧⌘U") + ">");
+ upElement.innerHTML = '';
+ upElement.className = "vditor-icon vditor-tooltipped vditor-tooltipped__n";
+ upElement.onclick = () => {
+ range.insertNode(document.createElement("wbr"));
+ previousElement.insertAdjacentElement("beforebegin", element);
+ setRangeByWbr(vditor.wysiwyg.element, range);
+ afterRenderEvent(vditor);
+ highlightToolbarWYSIWYG(vditor);
+ scrollCenter(vditor);
+ };
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", upElement);
+};
+
+const genDown = (range: Range, element: HTMLElement, vditor: IVditor) => {
+ const nextElement = element.nextElementSibling;
+ if (
+ !nextElement ||
+ (!element.parentElement.isEqualNode(vditor.wysiwyg.element) &&
+ element.tagName !== "LI")
+ ) {
+ return;
+ }
+ const downElement = document.createElement("button");
+ downElement.setAttribute("type", "button");
+ downElement.setAttribute("data-type", "down");
+ downElement.setAttribute("aria-label", window.VditorI18n.down + "<" + updateHotkeyTip("⇧⌘D") + ">");
+ downElement.innerHTML =
+ '';
+ downElement.className =
+ "vditor-icon vditor-tooltipped vditor-tooltipped__n";
+ downElement.onclick = () => {
+ range.insertNode(document.createElement("wbr"));
+ nextElement.insertAdjacentElement("afterend", element);
+ setRangeByWbr(vditor.wysiwyg.element, range);
+ afterRenderEvent(vditor);
+ highlightToolbarWYSIWYG(vditor);
+ scrollCenter(vditor);
+ };
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", downElement);
+};
+
+const genClose = (element: HTMLElement, vditor: IVditor) => {
+ const close = document.createElement("button");
+ close.setAttribute("type", "button");
+ close.setAttribute("data-type", "remove");
+ close.setAttribute("aria-label", window.VditorI18n.remove + "<" + updateHotkeyTip("⇧⌘X") + ">");
+ close.innerHTML =
+ '';
+ close.className = "vditor-icon vditor-tooltipped vditor-tooltipped__n";
+ close.onclick = () => {
+ const range = getEditorRange(vditor);
+ range.setStartAfter(element);
+ setSelectionFocus(range);
+ element.remove();
+ afterRenderEvent(vditor);
+ highlightToolbarWYSIWYG(vditor);
+ };
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", close);
+};
+
+const linkHotkey = (
+ vditor: IVditor,
+ element: HTMLElement,
+ event: KeyboardEvent,
+ nextInputElement: HTMLInputElement,
+) => {
+ if (event.isComposing) {
+ return;
+ }
+ if (event.key === "Tab") {
+ nextInputElement.focus();
+ nextInputElement.select();
+ event.preventDefault();
+ return;
+ }
+ if (
+ !isCtrl(event) &&
+ !event.shiftKey &&
+ event.altKey &&
+ event.key === "Enter"
+ ) {
+ const range = getEditorRange(vditor);
+ // firefox 不会打断 link https://github.com/Vanessa219/vditor/issues/193
+ element.insertAdjacentHTML("afterend", Constants.ZWSP);
+ range.setStartAfter(element.nextSibling);
+ range.collapse(true);
+ setSelectionFocus(range);
+ event.preventDefault();
+ }
+};
+
+export const genAPopover = (vditor: IVditor, aElement: HTMLElement) => {
+ const lang: keyof II18n | "" = vditor.options.lang;
+ const options: IOptions = vditor.options;
+ vditor.wysiwyg.popover.innerHTML = "";
+
+ const updateA = () => {
+ if (input.value.trim() !== "") {
+ aElement.innerHTML = input.value;
+ }
+ aElement.setAttribute("href", input1.value);
+ aElement.setAttribute("title", input2.value);
+ };
+
+ aElement.querySelectorAll("[data-marker]").forEach((item: HTMLElement) => {
+ item.removeAttribute("data-marker");
+ });
+ const inputWrap = document.createElement("span");
+ inputWrap.setAttribute("aria-label", window.VditorI18n.textIsNotEmpty);
+ inputWrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const input = document.createElement("input");
+ inputWrap.appendChild(input);
+ input.className = "vditor-input";
+ input.setAttribute("placeholder", window.VditorI18n.textIsNotEmpty);
+ input.style.width = "120px";
+ input.value = aElement.innerHTML || "";
+ input.oninput = () => {
+ updateA();
+ };
+ input.onkeydown = (event) => {
+ if (removeBlockElement(vditor, event)) {
+ return;
+ }
+ linkHotkey(vditor, aElement, event, input1);
+ };
+
+ const input1Wrap = document.createElement("span");
+ input1Wrap.setAttribute("aria-label", window.VditorI18n.link);
+ input1Wrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const input1 = document.createElement("input");
+ input1Wrap.appendChild(input1);
+ input1.className = "vditor-input";
+ input1.setAttribute("placeholder", window.VditorI18n.link);
+ input1.value = aElement.getAttribute("href") || "";
+ input1.oninput = () => {
+ updateA();
+ };
+ input1.onkeydown = (event) => {
+ if (removeBlockElement(vditor, event)) {
+ return;
+ }
+ linkHotkey(vditor, aElement, event, input2);
+ };
+
+ const input2Wrap = document.createElement("span");
+ input2Wrap.setAttribute("aria-label", window.VditorI18n.tooltipText);
+ input2Wrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const input2 = document.createElement("input");
+ input2Wrap.appendChild(input2);
+ input2.className = "vditor-input";
+ input2.setAttribute("placeholder", window.VditorI18n.tooltipText);
+ input2.style.width = "60px";
+ input2.value = aElement.getAttribute("title") || "";
+ input2.oninput = () => {
+ updateA();
+ };
+ input2.onkeydown = (event) => {
+ if (removeBlockElement(vditor, event)) {
+ return;
+ }
+ linkHotkey(vditor, aElement, event, input);
+ };
+
+ genClose(aElement, vditor);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", inputWrap);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", input1Wrap);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", input2Wrap);
+ setPopoverPosition(vditor, aElement);
+};
+
+export const genImagePopover = (event: Event, vditor: IVditor) => {
+ const imgElement = event.target as HTMLImageElement;
+ vditor.wysiwyg.popover.innerHTML = "";
+ const updateImg = () => {
+ imgElement.setAttribute("src", inputElement.value);
+ imgElement.setAttribute("alt", alt.value);
+ imgElement.setAttribute("title", title.value);
+ };
+
+ const inputWrap = document.createElement("span");
+ inputWrap.setAttribute("aria-label", window.VditorI18n.imageURL);
+ inputWrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const inputElement = document.createElement("input");
+ inputWrap.appendChild(inputElement);
+ inputElement.className = "vditor-input";
+ inputElement.setAttribute("placeholder", window.VditorI18n.imageURL);
+ inputElement.value = imgElement.getAttribute("src") || "";
+ inputElement.oninput = () => {
+ updateImg();
+ };
+ inputElement.onkeydown = (elementEvent) => {
+ removeBlockElement(vditor, elementEvent);
+ };
+
+ const altWrap = document.createElement("span");
+ altWrap.setAttribute("aria-label", window.VditorI18n.alternateText);
+ altWrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const alt = document.createElement("input");
+ altWrap.appendChild(alt);
+ alt.className = "vditor-input";
+ alt.setAttribute("placeholder", window.VditorI18n.alternateText);
+ alt.style.width = "52px";
+ alt.value = imgElement.getAttribute("alt") || "";
+ alt.oninput = () => {
+ updateImg();
+ };
+ alt.onkeydown = (elementEvent) => {
+ removeBlockElement(vditor, elementEvent);
+ };
+
+ const titleWrap = document.createElement("span");
+ titleWrap.setAttribute("aria-label", window.VditorI18n.title);
+ titleWrap.className = "vditor-tooltipped vditor-tooltipped__n";
+ const title = document.createElement("input");
+ titleWrap.appendChild(title);
+ title.className = "vditor-input";
+ title.setAttribute("placeholder", window.VditorI18n.title);
+ title.value = imgElement.getAttribute("title") || "";
+ title.oninput = () => {
+ updateImg();
+ };
+ title.onkeydown = (elementEvent) => {
+ removeBlockElement(vditor, elementEvent);
+ };
+ genClose(imgElement, vditor);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", inputWrap);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", altWrap);
+ vditor.wysiwyg.popover.insertAdjacentElement("beforeend", titleWrap);
+
+ setPopoverPosition(vditor, imgElement);
+};
diff --git a/src/libs/vditor/src/ts/wysiwyg/index.ts b/src/libs/vditor/src/ts/wysiwyg/index.ts
new file mode 100644
index 00000000..5c538f4e
--- /dev/null
+++ b/src/libs/vditor/src/ts/wysiwyg/index.ts
@@ -0,0 +1,547 @@
+import {Constants} from "../constants";
+import {hidePanel} from "../toolbar/setToolbar";
+import {isCtrl, isFirefox} from "../util/compatibility";
+import {
+ blurEvent,
+ copyEvent, cutEvent, dblclickEvent,
+ dropEvent,
+ focusEvent,
+ hotkeyEvent,
+ scrollCenter,
+ selectEvent,
+} from "../util/editorCommonEvent";
+import {isHeadingMD, isHrMD, paste} from "../util/fixBrowserBehavior";
+import {
+ hasClosestBlock, hasClosestByAttribute,
+ hasClosestByClassName, hasClosestByMatchTag,
+} from "../util/hasClosest";
+import {hasClosestByHeadings} from "../util/hasClosestByHeadings";
+import {
+ getCursorPosition,
+ getEditorRange,
+ getSelectPosition,
+ setRangeByWbr,
+} from "../util/selection";
+import {clickToc, renderToc} from "../util/toc";
+import {afterRenderEvent} from "./afterRenderEvent";
+import {genImagePopover, genLinkRefPopover, highlightToolbarWYSIWYG} from "./highlightToolbarWYSIWYG";
+import {getRenderElementNextNode, modifyPre} from "./inlineTag";
+import {input} from "./input";
+import {showCode} from "./showCode";
+
+class WYSIWYG {
+ public range: Range;
+ public element: HTMLPreElement;
+ public popover: HTMLDivElement;
+ public selectPopover: HTMLDivElement;
+ public afterRenderTimeoutId: number;
+ public hlToolbarTimeoutId: number;
+ public preventInput: boolean;
+ public composingLock = false;
+ public commentIds: string[] = [];
+
+ constructor(vditor: IVditor) {
+ const divElement = document.createElement("div");
+ divElement.className = "vditor-wysiwyg";
+
+ divElement.innerHTML = `
+
+ ";
+ }
+ for (let l = 0; l < rowDiff; l++) {
+ if (tableElement.querySelector("tbody")) {
+ tableElement
+ .querySelector("tbody")
+ .insertAdjacentHTML("beforeend", rowHTML);
+ } else {
+ tableElement
+ .querySelector("thead")
+ .insertAdjacentHTML("afterend", rowHTML + "/g, "");
+
+ if (html === '
";
+ }
+ if (blockElement.tagName === "BLOCKQUOTE" || blockElement.classList.contains("vditor-reset")) {
+ blockElement.innerHTML = `<${tagName} data-block="0">${blockElement.innerHTML.trim()}${tagName}>`;
+ } else {
+ blockElement.outerHTML = `<${tagName} data-block="0">${blockElement.innerHTML.trim()}${tagName}>`;
+ }
+ setRangeByWbr(vditor.wysiwyg.element, range);
+ renderToc(vditor);
+ }
+};
+
+export const removeHeading = (vditor: IVditor) => {
+ const range = getSelection().getRangeAt(0);
+ let blockElement = hasClosestBlock(range.startContainer);
+ if (!blockElement) {
+ blockElement = range.startContainer.childNodes[range.startOffset] as HTMLElement;
+ }
+ if (blockElement) {
+ range.insertNode(document.createElement("wbr"));
+ blockElement.outerHTML = `${liElement.innerHTML}
`;
+ } else {
+ blockElement.outerHTML = `${blockElement.outerHTML}
`;
+ }
+ setRangeByWbr(vditor.wysiwyg.element, range);
+ }
+ } else if (commandName === "check" || commandName === "list" || commandName === "ordered-list") {
+ listToggle(vditor, range, commandName, false);
+ setRangeByWbr(vditor.wysiwyg.element, range);
+ useHighlight = false;
+ removeCurrentToolbar(vditor.toolbar.elements, ["check", "list", "ordered-list"]);
+ actionBtn.classList.add("vditor-menu--current");
+ } else if (commandName === "inline-code") {
+ if (range.toString() === "") {
+ const node = document.createElement("code");
+ node.textContent = Constants.ZWSP;
+ range.insertNode(node);
+ range.setStart(node.firstChild, 1);
+ range.collapse(true);
+ setSelectionFocus(range);
+ } else if (range.startContainer.nodeType === 3) {
+ const node = document.createElement("code");
+ range.surroundContents(node);
+ range.insertNode(node);
+ setSelectionFocus(range);
+ }
+ actionBtn.classList.add("vditor-menu--current");
+ } else if (commandName === "code") {
+ const node = document.createElement("div");
+ node.className = "vditor-wysiwyg__block";
+ node.setAttribute("data-type", "code-block");
+ node.setAttribute("data-block", "0");
+ node.setAttribute("data-marker", "```");
+ if (range.toString() === "") {
+ node.innerHTML = "
";
+ } else {
+ node.innerHTML = `
`;
+ range.deleteContents();
+ }
+ range.insertNode(node);
+ if (blockElement) {
+ blockElement.outerHTML = vditor.lute.SpinVditorDOM(blockElement.outerHTML);
+ }
+ setRangeByWbr(vditor.wysiwyg.element, range);
+ vditor.wysiwyg.element.querySelectorAll(".vditor-wysiwyg__preview[data-render='2']").forEach(
+ (item: HTMLElement) => {
+ processCodeRender(item, vditor);
+ });
+ actionBtn.classList.add("vditor-menu--disabled");
+ } else if (commandName === "link") {
+ if (range.toString() === "") {
+ const aElement = document.createElement("a");
+ aElement.innerText = Constants.ZWSP;
+ range.insertNode(aElement);
+ range.setStart(aElement.firstChild, 1);
+ range.collapse(true);
+ genAPopover(vditor, aElement);
+ const textInputElement = vditor.wysiwyg.popover.querySelector("input");
+ textInputElement.value = "";
+ textInputElement.focus();
+ useRender = false;
+ } else {
+ const node = document.createElement("a");
+ node.setAttribute("href", "");
+ node.innerHTML = range.toString();
+ range.surroundContents(node);
+ range.insertNode(node);
+ setSelectionFocus(range);
+ genAPopover(vditor, node);
+ const textInputElements = vditor.wysiwyg.popover.querySelectorAll("input");
+ textInputElements[0].value = node.innerText;
+ textInputElements[1].focus();
+ }
+ useHighlight = false;
+ actionBtn.classList.add("vditor-menu--current");
+ } else if (commandName === "table") {
+ let tableHTML = `${range.toString()}
`;
+ if (range.toString().trim() === "") {
+ if (blockElement && blockElement.innerHTML.trim().replace(Constants.ZWSP, "") === "") {
+ blockElement.outerHTML = tableHTML;
+ } else {
+ document.execCommand("insertHTML", false, tableHTML);
+ }
+ range.selectNode(vditor.wysiwyg.element.querySelector("wbr").previousSibling);
+ vditor.wysiwyg.element.querySelector("wbr").remove();
+ setSelectionFocus(range);
+ } else {
+ tableHTML = `col1 col2 col3
";
+ document.execCommand("insertHTML", false, tableHTML);
+ setRangeByWbr(vditor.wysiwyg.element, range);
+ }
+ useHighlight = false;
+ actionBtn.classList.add("vditor-menu--disabled");
+ } else if (commandName === "line") {
+ if (blockElement) {
+ const hrHTML = '`;
+ const tableText = range.toString().split("\n");
+ const delimiter = tableText[0].split(",").length > tableText[0].split("\t").length ? "," : "\t";
+
+ tableText.forEach((rows, index) => {
+ if (index === 0) {
+ rows.split(delimiter).forEach((header, subIndex) => {
+ if (subIndex === 0) {
+ tableHTML += ` ";
+ } else {
+ if (index === 1) {
+ tableHTML += "${header} `;
+ } else {
+ tableHTML += `${header} `;
+ }
+ });
+ tableHTML += "";
+ } else {
+ tableHTML += " ";
+ }
+ rows.split(delimiter).forEach((cell) => {
+ tableHTML += ` `;
+ }
+ });
+ tableHTML += "${cell} `;
+ });
+ tableHTML += `