MCPcopy Create free account
hub / github.com/cweijan/vscode-office / mountCodeMirror

Function mountCodeMirror

vditor/src/ts/codeBlock/codeMirrorManager.ts:1572–1649  ·  view source on GitHub ↗
(blockElement: HTMLElement, vditor: IVditor, force = false)

Source from the content-addressed store, hash-verified

1570 });
1571};
1572
1573const mountCodeMirror = (blockElement: HTMLElement, vditor: IVditor, force = false) => {
1574 if (!force && !isCmCodeBlock(blockElement)) {
1575 return;
1576 }
1577
1578 cancelLazyTeardown(blockElement);
1579 blockElement.classList.remove(CM_PLACEHOLDER_CLASS);
1580
1581 cleanupStaleCmArtifacts(blockElement);
1582 prepareCmBlockDom(blockElement, isSpecialBlock(blockElement));
1583
1584 const parts = getBlockParts(blockElement);
1585 if (!parts) {
1586 return;
1587 }
1588 if (isMathBlockElement(blockElement)) {
1589 parts.code.classList.add("language-math");
1590 }
1591
1592 const existing = bindings.get(blockElement);
1593 if (existing) {
1594 syncViewFromCode(existing);
1595 const languageName = getLanguageName(parts.code);
1596 if (existing.languageName !== languageName) {
1597 existing.syncCode.className = parts.code.className;
1598 applyLanguage(blockElement, existing, languageName);
1599 }
1600 ensureCodeBlockChrome(vditor, blockElement, {
1601 performCopy: () => copyCodeMirrorView(existing.view, existing.languageName),
1602 });
1603 updateCodeBlockChromeLanguage(blockElement, languageName);
1604 return;
1605 }
1606
1607 const { editPre, code } = parts;
1608 const languageCompartment = new Compartment();
1609 const languageName = getLanguageName(code);
1610
1611 const binding: CodeMirrorBinding = {
1612 view: null as unknown as EditorView,
1613 languageCompartment,
1614 editPre,
1615 syncCode: code,
1616 updating: false,
1617 syncTimer: 0,
1618 previewTimer: 0,
1619 languageName: "",
1620 };
1621
1622 const rawCode = code.textContent || "";
1623
1624 const view = new EditorView({
1625 // Lute有bug, 会给代码块末尾生成一行空行
1626 doc: rawCode.trimEnd(),
1627 parent: editPre,
1628 extensions: [
1629 vditorCodeMirrorSetup,

Callers 7

scheduleSpecialBlockFunction · 0.85
scheduleLazyCodeBlockFunction · 0.85
enterSpecialBlockEditFunction · 0.85
restoreCodeMirrorFocusFunction · 0.85
focusCodeMirrorFunction · 0.85

Calls 15

ensureCodeBlockChromeFunction · 0.90
isCmCodeBlockFunction · 0.85
cancelLazyTeardownFunction · 0.85
cleanupStaleCmArtifactsFunction · 0.85
prepareCmBlockDomFunction · 0.85
isSpecialBlockFunction · 0.85
getBlockPartsFunction · 0.85
isMathBlockElementFunction · 0.85
syncViewFromCodeFunction · 0.85
getLanguageNameFunction · 0.85

Tested by

no test coverage detected