(
blockElement: HTMLElement,
collapseToStart = true,
vditor?: IVditor,
)
| 1692 | preserveEditorScroll(vditor, apply); |
| 1693 | }; |
| 1694 | |
| 1695 | export const focusCodeMirror = ( |
| 1696 | blockElement: HTMLElement, |
| 1697 | collapseToStart = true, |
| 1698 | vditor?: IVditor, |
| 1699 | ) => { |
| 1700 | if (!blockElement) { |
| 1701 | return; |
| 1702 | } |
| 1703 | if (!bindings.get(blockElement) && vditor) { |
| 1704 | if (isLazyManagedCodeBlock(blockElement)) { |
| 1705 | enforceMaxMounted(vditor, blockElement); |
| 1706 | } |
| 1707 | mountCodeMirror(blockElement, vditor, true); |
| 1708 | } |
| 1709 | const binding = bindings.get(blockElement); |
| 1710 | if (!binding) { |
| 1711 | return; |
| 1712 | } |
| 1713 | const hadFocus = binding.view.hasFocus; |
| 1714 | const applyFocus = () => { |
| 1715 | focusCmViewWithoutScroll(binding.view); |
| 1716 | if (!hadFocus) { |
| 1717 | if (collapseToStart) { |
| 1718 | binding.view.dispatch({ |
| 1719 | selection: { anchor: 0, head: 0 }, |
| 1720 | scrollIntoView: false, |
| 1721 | }); |
| 1722 | } else { |
| 1723 | const length = binding.view.state.doc.length; |
| 1724 | binding.view.dispatch({ |
| 1725 | selection: { anchor: length, head: length }, |
| 1726 | scrollIntoView: false, |
| 1727 | }); |
| 1728 | } |
| 1729 | } |
| 1730 | }; |
| 1731 | if (vditor) { |
| 1732 | preserveEditorScroll(vditor, applyFocus); |
| 1733 | } else { |
| 1734 | applyFocus(); |
| 1735 | } |
| 1736 | }; |
| 1737 | |
| 1738 | /** @deprecated use focusCodeMirror */ |
no test coverage detected