pax_global_header00006660000000000000000000000064150353732520014517gustar00rootroot0000000000000052 comment=fcc4a72042ff0aae27101bcd564658f4646d0278 view-6.38.1/000077500000000000000000000000001503537325200125705ustar00rootroot00000000000000view-6.38.1/.github/000077500000000000000000000000001503537325200141305ustar00rootroot00000000000000view-6.38.1/.github/workflows/000077500000000000000000000000001503537325200161655ustar00rootroot00000000000000view-6.38.1/.github/workflows/dispatch.yml000066400000000000000000000006371503537325200205150ustar00rootroot00000000000000name: Trigger CI on: push jobs: build: name: Dispatch to main repo runs-on: ubuntu-latest steps: - name: Emit repository_dispatch uses: mvasigh/dispatch-action@main with: # You should create a personal access token and store it in your repository token: ${{ secrets.DISPATCH_AUTH }} repo: dev owner: codemirror event_type: push view-6.38.1/.gitignore000066400000000000000000000001271503537325200145600ustar00rootroot00000000000000/node_modules package-lock.json /dist /test/*.js /test/*.d.ts /test/*.d.ts.map .tern-* view-6.38.1/.npmignore000066400000000000000000000001001503537325200145560ustar00rootroot00000000000000/src /test /node_modules .tern-* rollup.config.js tsconfig.json view-6.38.1/CHANGELOG.md000066400000000000000000002121271503537325200144060ustar00rootroot00000000000000## 6.38.1 (2025-07-15) ### Bug fixes Make the keymap not dispatch Alt key combos on macOS by key code, because those are generally used to type special characters. Fix a layout bug that could occur with very narrow editors. ## 6.38.0 (2025-06-27) ### New features Gutters can now specify that they should be displayed after the content (which would be to the right in a left-to-right layout). ## 6.37.2 (2025-06-12) ### Bug fixes Fix an issue where moving the cursor vertically from the one-but-last character on a line would sometimes move incorrectly on Safari. Fix an issue causing coordinates between lines of text to sometimes be inappropriately placed at the end of the line by `posAtCoords`. ## 6.37.1 (2025-05-30) ### Bug fixes Properly add `crelt` as a dependency. ## 6.37.0 (2025-05-29) ### New features View plugins can now take an argument, in which case they must be instantiated with their `of` method in order to be added to a configuration. The new `showDialog` function makes it easy to show a notification or prompt using a CodeMirror panel. ## 6.36.8 (2025-05-12) ### Bug fixes Make `logException` log errors to the console when `onerror` returns a falsy value. Fix an issue in `MatchDecorator` causing `updateDeco` to sometimes not do the right thing for deletions. ## 6.36.7 (2025-05-02) ### Bug fixes Use the `aria-placeholder` attribute to communicate the placeholder text to screen readers. Fix a crash when `EditorView.composing` or `.compositionStarted` are accessed during view initialization. ## 6.36.6 (2025-04-24) ### Bug fixes Fix an issue where `drawSelection` would draw selections starting at a block widget not at a line break in an odd way. Fix an issue where the editor would inappropriately scroll when editing near the bottom of the document with line wrapping enabled, in some cases. Fix an issue that caused unnecessary transactions on focus change. ## 6.36.5 (2025-03-29) ### Bug fixes Fix an issue where some browsers wouldn't enable context menu paste when clicking on placeholder text. Fix an issue where cursor height would unnecessarily be based on a placeholder node's dimensions, and thus be off from the text height. ## 6.36.4 (2025-03-03) ### Bug fixes Fix an issue where scrolling down to a range higher than the viewport could in some situations fail to scroll to the proper position. ## 6.36.3 (2025-02-18) ### Bug fixes Make sure event handlers registered with `domEventHandlers` are not called during view updates, to avoid triggering nested update errors. Don't include the window scrollbars in the space available for displaying tooltips. Work around an issue with Chrome's `EditContext` that shows up when using autocompletion while composing with Samsung's virtual Android keyboard. ## 6.36.2 (2025-01-09) ### Bug fixes Fix an issue where some kinds of relayouts could put the editor in a state where it believed it wasn't in window, preventing relayout, though it in fact was. Make sure macOS double-space-to-period conversions are properly suppressed. Fix an issue where native selection changes, such as mobile spacebar-drag, weren't being picked up in edit context mode. ## 6.36.1 (2024-12-19) ### Bug fixes Fix a crash in MatchDecorator when updating matches at the end of the document. ## 6.36.0 (2024-12-17) ### Bug fixes Make selection rectangles verticaly align precisely, rather than introducing a slight overlap. Fix an issue in `MatchDecorator` that caused it to fully rebuild its decorations on normal edits. ### New features View updates now have a `viewportMoved` flag that is only true when a viewport change originated from something other than mapping the viewport over a document change. ## 6.35.3 (2024-12-09) ### Bug fixes Fix an issue where mark decorations that got merged or split weren't properly redrawn. Avoid spurious focus events by not updating the DOM selection when the editor is unfocused but focusable. Disable `writingsuggestions` for the editable element, to opt out of Safari's new intelligence completions (which mess up in the editor). ## 6.35.2 (2024-12-07) ### Bug fixes Fix an issue on Chrome where typing at the end of the document would insert a character after the cursor. ## 6.35.1 (2024-12-06) ### Bug fixes Work around another crash caused by incorrect composition positions reported by `EditContext`. Stop disabling custom cursors on Safari version 11.4 and up, which support `caret-color`. Fix an issue where a tooltip with wrapped content could, in some circumstances, fail to find a stable position due to a cyclic dependency between its width and its position. ## 6.35.0 (2024-11-21) ### New features Tooltips can now use the `clip` option to control whether they are hidden when outside the visible editor content. ## 6.34.3 (2024-11-15) ### Bug fixes Make sure positions covered by a gutter or a panel aren't treated as visible for the purpose of displaying tooltips. Properly include the tooltip arrow height when checking whether a tooltip fits in its preferred above/below position. Fix an issue with compositions on Chrome inserting their content in the wrong position when another document change came in during composition. ## 6.34.2 (2024-11-05) ### Bug fixes Fix the default cursor color for dark themes, which was way too dark. ## 6.34.1 (2024-09-27) ### Bug fixes Avoid a stack overflow that could happen when updating a line with a lot of text tokens. Improve the way enormously long (non-wrapped) lines are displayed by making sure they stay shorter than the maximal pixel size the browser's CSS engine can handle. ## 6.34.0 (2024-09-25) ### Bug fixes Fix an issue where the dots past the wrapping point were displayed incorrectly when using `highlightWhitespace` with a wrapped sequence of spaces. Improve performance of documents displaying lots of highlighted spaces by using a CSS background instead of pseudo-element. ### New features `placeholder` now allows a function that constructs the placedholder DOM to be passed in, and uses `cloneNode` when a raw element is passed in, to prevent adding the same element to multiple editors. ## 6.33.1 (2024-08-30) ### Bug fixes Work around odd behavior in Chrome's newly supported `caretPositionFromPoint` method, which could cause CodeMirror to crash with a null dereference. ## 6.33.0 (2024-08-24) ### Bug fixes Make it easier to move the pointer over a hover tooltip with an arrow by not closing the tooltip when the pointer is moving over the gap for the arrow. ### New features The new `EditorView.clipboardInputFilter` and `clipboardOutputFilter` facets allow you to register filter functions that change text taken from or sent to the clipboard. ## 6.32.0 (2024-08-12) ### Bug fixes Fix a bug where the editor could draw way too big a viewport when not managing its own scrollbar. ### New features The new `gutterWidgetClass` facet makes it possible to add a class to gutter elements next to widgets. ## 6.31.0 (2024-08-11) ### Bug fixes Avoid the editor's geometry measurements becoming incorrect when fonts finish loading by scheduling a measure on `document.fonts.ready`. Avoid an issue where Chrome would incorrectly scroll the window when deleting lines in the editor. Fix an issue where in some layouts editor content would be drawn on top of panel elements. Fix an issue where `coordsAtPos` would return null when querying a position in a block widget. ### New features The new `lineNumberWidgetMarker` facet makes it possible to insert markers into the line number gutter for widgets. ## 6.30.0 (2024-08-05) ### Bug fixes Make spell check corrections work again on `EditContext`-enabled Chrome versions. ### New features The value returned by `hoverTooltip` now has an `active` property providing the state field used to store the open tooltips. ## 6.29.1 (2024-07-29) ### Bug fixes Fix a crash on old Safari browsers that don't support `MediaQueryList.addEventListener`. Fix an issue where `EditorView.viewportLineBlocks` (and thus other things like the gutter) might be out of date after some kinds of decoration changes. ## 6.29.0 (2024-07-25) ### Bug fixes Fix an issue that caused typing into an editor marked read-only to cause document changes when using `EditContext`. Associate a cursor created by clicking above the end of the text on a wrap point with the line before it. ### New features The package now exports the type of hover tooltip sources as `HoverTooltipSource`. ## 6.28.6 (2024-07-19) ### Bug fixes Fix an issue where the editor got confused about the position of inserted text when using Chrome's `EditContext` and canceling transactions for typed text. ## 6.28.5 (2024-07-17) ### Bug fixes Fix a bug that broke drag scrolling along one axis when the innermost scrollable element around the editor was only scrollable along the other axis. Work around a memory leak in Chrome's EditContext implementation. ## 6.28.4 (2024-07-03) ### Bug fixes Fix a bug where EditContext-based editing could corrupt the document in some situations. ## 6.28.3 (2024-07-01) ### Bug fixes Fix an issue causing the IME interface to appear in the wrong spot on Chrome Windows. ## 6.28.2 (2024-06-21) ### Bug fixes Only use `EditContext` on Chrome versions that support passing it an inverted selection range. Fix an issue that prevented non-inclusive block widgets from having their `updateDOM` method called when changed. Re-enable `EditContext` use on Chrome 126 and up. ## 6.28.1 (2024-06-12) ### Bug fixes Disable `EditContext` by default again, to work around a regression where Chrome's implementation doesn't support inverted selections. Make sure `EditorView.editable` is respected when `EditContext` is used. ## 6.28.0 (2024-06-10) ### Bug fixes Fix an issue where long lines broken up by block widgets were sometimes only partially rendered. ### New features The editor will now, when available (which is only on Chrome for the foreseeable future) use the [`EditContext`](https://developer.mozilla.org/en-US/docs/Web/API/EditContext) API to capture text input. ## 6.27.0 (2024-06-04) ### New features The new `setTabFocusMode` method can be used to control whether the editor disables key bindings for Tab and Shift-Tab. ## 6.26.4 (2024-06-04) ### Bug fixes Fix an issue where commands with an optional second argument would get the keyboard event in that argument when called from a keymap. Fix an issue that could cause the cursor to be rendered on the wrong side of a zero-length block widget. Fix an issue where `drawSelection` got confused by block widgets in line-wrapped editors in some situations. Don't hide the native selection in widgets that have focus. Make sure that clicking an unfocusable editor still remove focus from any other focused elements. Fix a crash when loading the package in a non-browser environment. Stop mouse selection when the user types. ## 6.26.3 (2024-04-12) ### Bug fixes Fix an issue where dispatching an update to an editor before it measured itself for the first time could cause the scroll position to incorrectly move. Fix a crash when multiple tooltips with arrows are shown. ## 6.26.2 (2024-04-09) ### Bug fixes Improve behavior of `scrollPastEnd` in a scaled editor. When available, use `Selection.getComposedRanges` on Safari to find the selection inside a shadow DOM. Remove the workaround that avoided inappropriate styling on composed text after a decoration again, since it breaks the stock Android virtual keyboard. ## 6.26.1 (2024-03-28) ### Bug fixes Fix the editor getting stuck in composition when Safari fails to fire a compositionend event for a dead key composition. Fix an issue where, with IME systems that kept the cursor at the start of the composed text, the editor misidentified the target node and disrupted composition. Fix a bug where in a line-wrapped editor, with some content, the initial scroll position would be off from the top of the document. ## 6.26.0 (2024-03-14) ### Bug fixes Avoid the editor getting confused when iOS autocorrects on pressing Enter and does the correction and the break insertion in two different events. Fix the pasting of copied URIs in iOS. Fix a bug where a scaled editor could keep performing unnecessary updates due to tiny differences in geometry values returned by the browser. Fix a bug where, on iOS with a physical keyboard, the modifiers for some keys weren't being passed to the keymaps. Work around the fact that Mobile Safari makes DOM changes before firing a key event when typing ctrl-d on an external keyboard. Fix an issue where some commands didn't properly scroll the cursor into view on Mobile Safari. Re-measure the document when print settings are changed on Chrome. ### New features The `EditorView.scrollHandler` facet can be used to override or extend the behavior of the editor when things are scrolled into view. ## 6.25.1 (2024-03-06) ### Bug fixes Fix accidental non-optional field in layer config objects. ## 6.25.0 (2024-03-04) ### Bug fixes Properly recognize Android GBoard enter presses that strip a space at the end of the line as enter. Fix a bug that caused the gutter to have the wrong height when the editor was scaled after construction. When starting a composition after a non-inclusive mark decoration, temporarily insert a widget that prevents the composed text from inheriting that mark's styles. Make sure the selection is repositioned when a transaction changes decorations without changing the document. ### New features View plugins can now provide a `docViewUpdate` method that is called whenever the document view is updated. Layers now take a `updateOnDocUpdate` option that controls whether they are automatically updated when the document view changes. ## 6.24.1 (2024-02-19) ### Bug fixes Fix a crash that happens when hover tooltips are active during changes, introduced in 6.24.0. ## 6.24.0 (2024-02-09) ### Bug fixes Fix an issue that broke context-menu select-all on Chrome when the viewport didn't cover the whole document. Make sure tooltips are ordered by extension precedence in the DOM. ### New features Hover tooltip sources may now return multiple tooltips. ## 6.23.1 (2024-01-24) ### Bug fixes Fix a bug that caused `Tooltip.above` to not take effect for tooltips that were already present when the tooltip plugin is initialized. Automatically reposition tooltips when their size changes. ## 6.23.0 (2023-12-28) ### Bug fixes Work around odd iOS Safari behavior when doing select all. Fix a composition interruption when an widget is inserted next to the cursor. Fix a crash in bidirectional cursor motion. Simplify visual motion through bidirectional text, fix several corner cases where it would work badly. Fix a bug that broke some bidi isolates not on the first line of the document. ### New features `EditorView.bidiIsolatedRanges` now supports automatically determining the direction of the range if not provided by the decoration. `EditorView.visualLineSide` can be used to find the visual end or start of a line with bidirectional text. The new `EditorView.outerDecorations` facet can be used to provide decorations that should always be at the bottom of the precedence stack. ## 6.22.3 (2023-12-13) ### Bug fixes Fix a bug that could cause tooltips to be unnecessarily be positioned absolutely. Make sure that, when an editor creates tooltips immediately on initialization, the editor is attached to the document when their `mount` callback is called. ## 6.22.2 (2023-12-08) ### Bug fixes Fix an issue in the bidirectional motion that could cause the cursor to get stuck in a loop when a zero-width non-joiner char was placed on a direction boundary. Fix a bug that corrupts the editor's internal view tree data structure on some types of edits, putting the editor in a broken state. ## 6.22.1 (2023-11-27) ### Bug fixes Call widget `destroy` methods when the entire editor is destroyed or reset. Work around an issue on Safari on macOS Sonoma that made the native cursor visible even when `drawSelection` is enabled. Fix an issue where, on some browsers, the screenreader announced text ended up in the printed document. Fix a bug where a hover tooltip could stick around even though the pointer was no longer on the editor when it was moved out over the tooltip. Fix an issue where hover tooltips could close when moving the mouse onto them due to mouse position rounding issues. ## 6.22.0 (2023-11-03) ### Bug fixes Exceptions raised by update listeners are now routed to the configured exception sink, if any. Fix an issue where passing large scroll margins to `scrollIntoView` would cause the measure loop to fail to terminate. Widgets that are draggable (and allow drag events through in their `ignoreEvent` implementation) can now use the editor's built-in drag/drop behavior. ### New features The new `scrollTo` option to `EditorView` allows an initial scroll position to be provided. The new `EditorView.scrollSnapshot` method returns an effect that can be used to reset to a previous scroll position. ## 6.21.4 (2023-10-24) ### Bug fixes Support the `offset`, `getCoords`, `overlap`, and `resize` properties on hover tooltips, as long as they aren't given conflicting values when there are multiple active hover tooltips. Fix a bug that caused tooltips in the default configuration to be positioned incorrectly on Chrome when the editor was transformed. ## 6.21.3 (2023-10-06) ### Bug fixes Fix an issue that caused `coordsForChar` to return the wrong rectangle for characters after a line wrap in Safari. Make the context menu work when clicking below the content in a fixed-height editor. Tooltips that have been put below/above their target position because there is no room on their default side now stay there on further updates. ## 6.21.2 (2023-10-02) ### Bug fixes Fix a regression that broke dragging text from inside the editor. ## 6.21.1 (2023-10-02) ### Bug fixes Fix a bug that could corrupt the DOM view for specific changes involving newlines and mark decorations. ## 6.21.0 (2023-09-29) ### Bug fixes Fix a bug that could cause zero-length widgets at the start of a line to be left in the view even after they were removed. ### New features `RectangleMarker`'s dimension properties are now public. ## 6.20.2 (2023-09-25) ### Bug fixes Fix an issue in the way the DOM selection is being read that could break backspacing of widgets on Android. Fix a bug where the editor could incorrectly computate its transform scale when it was small. ## 6.20.1 (2023-09-22) ### Bug fixes Fix a crash in plugin event handlers after dynamic reconfiguration. Fix an issue where, on Chrome, tooltips would no longer use fixed positioning. ## 6.20.0 (2023-09-20) ### Bug fixes Fix an issue that caused `repositionTooltips` to crash when it was called on an editor without tooltips. Fix an issue that caused the tooltip system to leave empty nodes in the DOM when an editor using the `parent` option to `tooltips` is destroyed. Fix a bug that regression mouse interaction with the area of a fixed-size editor that isn't covered by the content. Fix some issues with the way `moveVertically` behaved for positions on line wrap points. Fix a bug that could cause the document DOM to be incorrectly updated on some types of viewport changes. ### New features The new `getDrawSelectionConfig` function returns the `drawSelection` configuration for a given state. ## 6.19.0 (2023-09-14) ### Bug fixes Make sure the drop cursor is properly cleaned up even when another extension handles the drop event. Fix a crash related to non-inclusive replacing block decorations. ### New features The new `EditorView.domEventObservers` (and the corresponding option to view plugins) allows you to register functions that are always called for an event, regardless of whether other handlers handled it. ## 6.18.1 (2023-09-11) ### Bug fixes Fix an issue where the editor duplicated text when the browser moved content into the focused text node on composition. Make sure `widgetMarker` is called for gutters on lines covered by a block replace decoration. Fix an issue where the cursor could be shown in a position that doesn't allow a cursor when the selection is in a block widget. ## 6.18.0 (2023-09-05) ### New features The new `EditorView.scaleX` and `scaleY` properties return the CSS-transformed scale of the editor (or 1 when not scaled). The editor now supports being scaled with CSS. ## 6.17.1 (2023-08-31) ### Bug fixes Don't close the hover tooltip when the pointer moves over empty space caused by line breaks within the hovered range. Fix a bug where on Chrome Android, if a virtual keyboard was slow to apply a change, the editor could end up dropping it. Work around an issue where line-wise copy/cut didn't work in Firefox because the browser wasn't firing those events when nothing was selected. Fix a crash triggered by the way some Android IME systems update the DOM. Fix a bug that caused replacing a word by an emoji on Chrome Android to be treated as a backspace press. ## 6.17.0 (2023-08-28) ### Bug fixes Fix a bug that broke hover tooltips when hovering over a widget. ### New features The new `EditorView.cspNonce` facet can be used to provide a Content Security Policy nonce for the library's generated CSS. The new `EditorView.bidiIsolatedRanges` can be used to inform the editor about ranges styled as Unicode bidirection isolates, so that it can compute the character order correctly. `EditorView.dispatch` now also accepts an array of transactions to be applied together in a single view update. The new `dispatchTransactions` option to `new EditorView` now replaces the old (deprecated but still supported) `dispatch` option in a way that allows multiple transactions to be applied in one update. Input handlers are now passed an additional argument that they can use to retrieve the default transaction that would be applied for the insertion. ## 6.16.0 (2023-07-31) ### Bug fixes Fix an issue that made the gutter not stick in place when the editor was in a right-to-left context. ### New features The new `EditorView.coordsForChar` method returns the client rectangle for a given character in the editor. ## 6.15.3 (2023-07-18) ### Bug fixes Fix another crash regression for compositions before line breaks. ## 6.15.2 (2023-07-18) ### Bug fixes Fix the check that made sure compositions are dropped when the selection is moved. ## 6.15.1 (2023-07-18) ### Bug fixes Fix a regression that could cause the composition content to be drawn incorrectly. ## 6.15.0 (2023-07-17) ### Bug fixes Fix dragging a selection from inside the current selection on macOS. Fix an issue that could cause the scroll position to jump wildly Don't try to scroll fixed-positioned elements into view by scrolling their parent elements. Fix a bug that caused the cursor to be hidden when showing a placeholder that consisted of the empty string. Resolve some issues where composition could incorrectly affect nearby replaced content. ### New features Key bindings can now set a `stopPropagation` field to cause the view to stop the key event propagation when it considers the event handled. ## 6.14.1 (2023-07-06) ### Bug fixes Fix an issue where scrolling up through line-wrapped text would sometimes cause the scroll position to pop down. Fix an issue where clicking wouldn't focus the editor on Firefox when it was in an iframe and already the active element of the frame. Fix a bug that could cause compositions to be disrupted because their surrounding DOM was repurposed for some other piece of content. Fix a bug where adding content to the editor could inappropriately move the scroll position. Extend detection of Enter presses on Android to `beforeInput` events with an `"insertLineBreak"` type. ## 6.14.0 (2023-06-23) ### Bug fixes When dragging text inside the editor, look at the state of Ctrl (or Alt on macOS) at the time of the drop, not the start of drag, to determine whether to move or copy the text. Fix an issue where having a bunch of padding on lines could cause vertical cursor motion and `posAtCoords` to jump over lines. ### New features Block widget decorations can now be given an `inlineOrder` option to make them appear in the same ordering as surrounding inline widgets. ## 6.13.2 (2023-06-13) ### Bug fixes Fix an issue in scroll position stabilization for changes above the visible, where Chrome already does this natively and we ended up compensating twice. ## 6.13.1 (2023-06-12) ### Bug fixes Fix a bug where the cursor would in some circumstances be drawn on the wrong side of an inline widget. Fix an issue where `scrollPastEnd` could cause the scroll position of editors that weren't in view to be changed unnecessarily. ## 6.13.0 (2023-06-05) ### Bug fixes Forbid widget decoration side values bigger than 10000, to prevent them from breaking range ordering invariants. Fix a bug where differences between widgets' estimated and actual heights could cause the editor to inappropriately move the scroll position. Avoid another situation in which composition that inserts line breaks could corrupt the editor DOM. ### New features Inline widgets may now introduce line breaks, if they report this through the `WidgetType.lineBreaks` property. ## 6.12.0 (2023-05-18) ### Bug fixes Remove an accidentally included `console.log`. ### New features `EditorViewConfig.dispatch` is now passed the view object as a second argument. ## 6.11.3 (2023-05-17) ### Bug fixes Make sure pointer selection respects `EditorView.atomicRanges`. Preserve DOM widgets when their decoration type changes but they otherwise stay in the same place. Fix a bug in `drawSelection` that could lead to invisible or incorrect selections for a blank line below a block widget. ## 6.11.2 (2023-05-13) ### Bug fixes Fix a bug where the `crosshairCursor` extension could, when non-native key events were fired, trigger disruptive and needless view updates. Fix an Android issue where backspacing at the front of a line with widget decorations could replace those decorations with their text content. Respect scroll margins when scrolling the target of drag-selection into view. Validate selection offsets reported by the browser, to work around Safari giving us invalid values in some cases. ## 6.11.1 (2023-05-09) ### Bug fixes Don't preserve the DOM around a composition that spans multiple lines. ## 6.11.0 (2023-05-03) ### New features Gutters now support a `widgetMarker` option that can be used to add markers next to block widgets. ## 6.10.1 (2023-05-01) ### Bug fixes Limit cursor height in front of custom placeholder DOM elements. ## 6.10.0 (2023-04-25) ### Bug fixes Fix a crash in `drawSelection` when a measured position falls on a position that doesn't have corresponding screen coordinates. Work around unhelpful interaction observer behavior that could cause the editor to not notice it was visible. Give the cursor next to a line-wrapped placeholder a single-line height. Make sure drop events below the editable element in a fixed-height editor get handled properly. ### New features Widget decorations can now define custom `coordsAtPos` methods to control the way the editor computes screen positions at or in the widget. ## 6.9.6 (2023-04-21) ### Bug fixes Fix an issue where, when escape was pressed followed by a key that the editor handled, followed by tab, the tab would still move focus. Fix an issue where, in some circumstances, the editor would ignore text changes at the end of a composition. Allow inline widgets to be updated to a different length via `updateDOM`. ## 6.9.5 (2023-04-17) ### Bug fixes Avoid disrupting the composition in specific cases where Safari invasively changes the DOM structure in the middle of a composition. Fix a bug that prevented `destroy` being called on hover tooltips. Fix a bug where the editor could take focus when content changes required it to restore the DOM selection. Fix height layout corruption caused by a division by zero. Make sure styles targeting the editor's focus status are specific enough to not cause them to apply to editors nested inside another focused editor. This will require themes to adjust their selection background styles to match the new specificity. ## 6.9.4 (2023-04-11) ### Bug fixes Make the editor scroll while dragging a selection near its sides, even if the cursor isn't outside the scrollable element. Fix a bug that interrupted composition after widgets in some circumstances on Firefox. Make sure the last change in a composition has its user event set to `input.type.compose`, even if the `compositionend` event fires before the changes are applied. Make it possible to remove additional selection ranges by clicking on them with ctrl/cmd held, even if they aren't cursors. Keep widget buffers between widgets and compositions, since removing them confuses IME on macOS Firefox. Fix a bug where, for DOM changes that put the selection in the middle of the changed range, the editor incorrectly set its selection state. Fix a bug where `coordsAtPos` could return a coordinates before the line break when querying a line-wrapped position with a positive `side`. ## 6.9.3 (2023-03-21) ### Bug fixes Work around a Firefox issue that caused `coordsAtPos` to return rectangles with the full line height on empty lines. Opening a context menu by clicking below the content element but inside the editor now properly shows the browser's menu for editable elements. Fix an issue that broke composition (especially of Chinese IME) after widget decorations. Fix an issue that would cause the cursor to jump around during compositions inside nested mark decorations. ## 6.9.2 (2023-03-08) ### Bug fixes Work around a Firefox CSS bug that caused cursors to stop blinking in a scrolled editor. Fix an issue in `drawSelection` where the selection extended into the editor's padding. Fix pasting of links copied from iOS share sheet. ## 6.9.1 (2023-02-17) ### Bug fixes Improve the way `posAtCoords` picks the side of a widget to return by comparing the coordinates the center of the widget. Fix an issue where transactions created for the `focusChangeEffect` facet were sometimes not dispatched. ## 6.9.0 (2023-02-15) ### Bug fixes Fix an issue where inaccurate estimated vertical positions could cause the viewport to not converge in line-wrapped editors. Don't suppress double-space to period conversion when autocorrect is enabled. Make sure the measuring code notices when the scaling of the editor is changed, and does a full measure in that case. ### New features The new `EditorView.focusChangeEffect` facet can be used to dispatch a state effect when the editor is focused or blurred. ## 6.8.1 (2023-02-08) ### Bug fixes Fix an issue where tooltips that have their height reduced have their height flicker when scrolling or otherwise interacting with the editor. ## 6.8.0 (2023-02-07) ### Bug fixes Fix a regression that caused clicking on the scrollbar to move the selection. Fix an issue where focus or blur event handlers that dispatched editor transactions could corrupt the mouse selection state. Fix a CSS regression that prevented the drop cursor from being positioned properly. ### New features `WidgetType.updateDOM` is now passed the editor view object. ## 6.7.3 (2023-01-12) ### Bug fixes Fix a bug in `posAtCoords` that could cause incorrect results for positions to the left of a wrapped line. ## 6.7.2 (2023-01-04) ### Bug fixes Fix a regression where the cursor didn't restart its blink cycle when moving it with the pointer. Even without a `key` property, measure request objects that are already scheduled will not be scheduled again by `requestMeasure`. Fix an issue where keymaps incorrectly interpreted key events that used Ctrl+Alt modifiers to simulate AltGr on Windows. Fix a bug where line decorations with a different `class` property would be treated as equal. Fix a bug that caused `drawSelection` to not notice when it was reconfigured. Fix a crash in the gutter extension caused by sharing of mutable arrays. Fix a regression that caused touch selection on mobile platforms to not work in an uneditable editor. Fix a bug where DOM events on the boundary between lines could get assigned to the wrong line. ## 6.7.1 (2022-12-12) ### Bug fixes Make the editor properly scroll when moving the pointer out of it during drag selection. Fix a regression where clicking below the content element in an editor with its own height didn't focus the editor. ## 6.7.0 (2022-12-07) ### Bug fixes Make the editor notice widget height changes to automatically adjust its height information. Fix an issue where widget buffers could be incorrectly omitted after empty lines. Fix an issue in content redrawing that could cause `coordsAtPos` to return incorrect results. ### New features The static `RectangleMarker.forRange` method exposes the logic used by the editor to draw rectangles covering a selection range. Layers can now provide a `destroy` function to be called when the layer is removed. The new `highlightWhitespace` extension makes spaces and tabs in the editor visible. The `highlightTrailingWhitespace` extension can be used to make trailing whitespace stand out. ## 6.6.0 (2022-11-24) ### New features The `layer` function can now be used to define extensions that draw DOM elements over or below the document text. Tooltips that are bigger than the available vertical space for them will now have their height set so that they don't stick out of the window. The new `resize` property on `TooltipView` can be used to opt out of this behavior. ## 6.5.1 (2022-11-15) ### Bug fixes Fix a bug that caused marked unnecessary splitting of mark decoration DOM elements in some cases. ## 6.5.0 (2022-11-14) ### Bug fixes Fix an issue where key bindings were activated for the wrong key in some situations with non-US keyboards. ### New features A tooltip's `positioned` callback is now passed the available space for tooltips. ## 6.4.2 (2022-11-10) ### Bug fixes Typing into a read-only editor no longer moves the cursor. Fix an issue where hover tooltips were closed when the mouse was moved over them if they had a custom parent element. Fix an issue where the editor could end up displaying incorrect height measurements (typically after initializing). ## 6.4.1 (2022-11-07) ### Bug fixes Fix an issue where coordinates next to replaced widgets were returned incorrectly, causing the cursor to be drawn in the wrong place. Update the `crosshairCursor` state on every mousemove event. Avoid an issue in the way that the editor enforces cursor associativity that could cause the cursor to get stuck on single-character wrapped lines. ## 6.4.0 (2022-10-18) ### Bug fixes Avoid an issue where `scrollPastEnd` makes a single-line editor have a vertical scrollbar. Work around a Chrome bug where it inserts a newline when you press space at the start of a wrapped line. Align `rectangularSelection`'s behavior with other popular editors by making it create cursors at the end of lines that are too short to touch the rectangle. Fix an issue where coordinates on mark decoration boundaries were sometimes taken from the wrong side of the position. Prevent scrolling artifacts caused by attempts to scroll stuff into view when the editor isn't being displayed. ### New features `TooltipView` objects can now provide a `destroy` method to be called when the tooltip is removed. ## 6.3.1 (2022-10-10) ### Bug fixes Fix a crash when trying to scroll something into view in an editor that wasn't in the visible DOM. Fix an issue where `coordsAtPos` returned the coordinates on the wrong side of a widget decoration wrapped in a mark decoration. Fix an issue where content on long wrapped lines could fail to properly scroll into view. Fix an issue where DOM change reading on Chrome Android could get confused when a transaction came in right after a beforeinput event for backspace, enter, or delete. ## 6.3.0 (2022-09-28) ### Bug fixes Reduce the amount of wrap-point jittering when scrolling through a very long wrapped line. Fix an issue where scrolling to content that wasn't currently drawn due to being on a very long line would often fail to scroll to the right position. Suppress double-space-adds-period behavior on Chrome Mac when it behaves weirdly next to widget. ### New features Key binding objects with an `any` property will now add handlers that are called for any key, within the ordering of the keybindings. ## 6.2.5 (2022-09-24) ### Bug fixes Don't override double/triple tap behavior on touch screen devices, so that the mobile selection menu pops up properly. Fix an issue where updating the selection could crash on Safari when the editor was hidden. ## 6.2.4 (2022-09-16) ### Bug fixes Highlight the active line even when there is a selection. Prevent the active line background from obscuring the selection backdrop. Fix an issue where elements with negative margins would confuse the editor's scrolling-into-view logic. Fix scrolling to a specific position in an editor that has not been in view yet. ## 6.2.3 (2022-09-08) ### Bug fixes Fix a bug where cursor motion, when starting from a non-empty selection range, could get stuck on atomic ranges in some circumstances. Avoid triggering Chrome Android's text-duplication issue when a period is typed in the middle of a word. ## 6.2.2 (2022-08-31) ### Bug fixes Don't reset the selection for selection change events that were suppressed by a node view. ## 6.2.1 (2022-08-25) ### Bug fixes Don't use the global `document` variable to track focus, since that doesn't work in another window/frame. Fix an issue where key handlers that didn't return true were sometimes called twice for the same keypress. Avoid editing glitches when using deletion keys like ctrl-d on iOS. Properly treat characters from the 'Arabic Presentation Forms-A' Unicode block as right-to-left. Work around a Firefox bug that inserts text at the wrong point for specific cross-line selections. ## 6.2.0 (2022-08-05) ### Bug fixes Fix a bug where `posAtCoords` would return the wrong results for positions to the right of wrapped lines. ### New features The new `EditorView.setRoot` method can be used when an editor view is moved to a new document or shadow root. ## 6.1.4 (2022-08-04) ### Bug fixes Make selection-restoration on focus more reliable. ## 6.1.3 (2022-08-03) ### Bug fixes Fix a bug where a document that contains only non-printing characters would lead to bogus text measurements (and, from those, to crashing). Make sure differences between estimated and actual block heights don't cause visible scroll glitches. ## 6.1.2 (2022-07-27) ### Bug fixes Fix an issue where double tapping enter to confirm IME input and insert a newline on iOS would sometimes insert two newlines. Fix an issue on iOS where a composition could get aborted if the editor scrolled on backspace. ## 6.1.1 (2022-07-25) ### Bug fixes Make `highlightSpecialChars` replace directional isolate characters by default. The editor will now try to suppress browsers' native behavior of resetting the selection in the editable content when the editable element is focused (programmatically, with tab, etc). Fix a CSS issue that made it possible, when the gutters were wide enough, for them to overlap with the content. ## 6.1.0 (2022-07-19) ### New features `MatchDecorator` now supports a `decorate` option that can be used to customize the way decorations are added for each match. ## 6.0.3 (2022-07-08) ### Bug fixes Fix a problem where `posAtCoords` could incorrectly return the start of the next line when querying positions between lines. Fix an issue where registering a high-precedence keymap made keymap handling take precedence over other keydown event handlers. Ctrl/Cmd-clicking can now remove ranges from a multi-range selection. ## 6.0.2 (2022-06-23) ### Bug fixes Fix a CSS issue that broke horizontal scroll width stabilization. Fix a bug where `defaultLineHeight` could get an incorrect value in very narrow editors. ## 6.0.1 (2022-06-17) ### Bug fixes Avoid DOM selection corruption when the editor doesn't have focus but has selection and updates its content. Fall back to dispatching by key code when a key event produces a non-ASCII character (so that Cyrillic and Arabic keyboards can still use bindings specified with Latin characters). ## 6.0.0 (2022-06-08) ### New features The new static `EditorView.findFromDOM` method can be used to retrieve an editor instance from its DOM structure. Instead of passing a constructed state to the `EditorView` constructor, it is now also possible to inline the configuration options to the state in the view config object. ## 0.20.7 (2022-05-30) ### Bug fixes Fix an issue on Chrome Android where the DOM could fail to display the actual document after backspace. Avoid an issue on Chrome Android where DOM changes were sometimes inappropriately replace by a backspace key effect due to spurious beforeinput events. Fix a problem where the content element's width didn't cover the width of the actual content. Work around a bug in Chrome 102 which caused wheel scrolling of the editor to be interrupted every few lines. ## 0.20.6 (2022-05-20) ### Bug fixes Make sure the editor re-measures itself when its attributes are updated. ## 0.20.5 (2022-05-18) ### Bug fixes Fix an issue where gutter elements without any markers in them would not get the `cm-gutterElement` class assigned. Fix an issue where DOM event handlers registered by plugins were retained indefinitely, even after the editor was reconfigured. ## 0.20.4 (2022-05-03) ### Bug fixes Prevent Mac-style behavior of inserting a period when the user inserts two spaces. Fix an issue where the editor would sometimes not restore the DOM selection when refocused with a selection identical to the one it held when it lost focus. ## 0.20.3 (2022-04-27) ### Bug fixes Fix a bug where the input handling could crash on repeated (or held) backspace presses on Chrome Android. ## 0.20.2 (2022-04-22) ### New features The new `hideOn` option to `hoverTooltip` allows more fine-grained control over when the tooltip should hide. ## 0.20.1 (2022-04-20) ### Bug fixes Remove debug statements that accidentally made it into 0.20.0. Fix a regression in `moveVertically`. ## 0.20.0 (2022-04-20) ### Breaking changes The deprecated interfaces `blockAtHeight`, `visualLineAtHeight`, `viewportLines`, `visualLineAt`, `scrollPosIntoView`, `scrollTo`, and `centerOn` were removed from the library. All decorations are now provided through `EditorView.decorations`, and are part of a single precedence ordering. Decoration sources that need access to the view are provided as functions. Atomic ranges are now specified through a facet (`EditorView.atomicRanges`). Scroll margins are now specified through a facet (`EditorView.scrollMargins`). Plugin fields no longer exist in the library (and are replaced by facets holding function values). This package no longer re-exports the Range type from @codemirror/state. ### Bug fixes Fix a bug where zero-length block widgets could cause `viewportLineBlocks` to contain overlapping ranges. ### New features The new `perLineTextDirection` facet configures whether the editor reads text direction per line, or uses a single direction for the entire editor. `EditorView.textDirectionAt` returns the direction around a given position. `rectangularSelection` and `crosshairCursor` from @codemirror/rectangular-selection were merged into this package. This package now exports the tooltip functionality that used to live in @codemirror/tooltip. The exports from the old @codemirror/panel package are now available from this package. The exports from the old @codemirror/gutter package are now available from this package. ## 0.19.48 (2022-03-30) ### Bug fixes Fix an issue where DOM syncing could crash when a DOM node was moved from a parent to a child node (via widgets reusing existing nodes). To avoid interfering with things like a vim mode too much, the editor will now only activate the tab-to-move-focus escape hatch after an escape press that wasn't handled by an event handler. Make sure the view measures itself before the page is printed. Tweak types of view plugin defining functions to avoid TypeScript errors when the plugin value doesn't have any of the interface's properties. ## 0.19.47 (2022-03-08) ### Bug fixes Fix an issue where block widgets at the start of the viewport could break height computations. ## 0.19.46 (2022-03-03) ### Bug fixes Fix a bug where block widgets on the edges of viewports could cause the positioning of content to misalign with the gutter and height computations. Improve cursor height next to widgets. Fix a bug where mapping positions to screen coordinates could return incorred coordinates during composition. ## 0.19.45 (2022-02-23) ### Bug fixes Fix an issue where the library failed to call `WidgetType.destroy` on the old widget when replacing a widget with a different widget of the same type. Fix an issue where the editor would compute DOM positions inside composition contexts incorrectly in some cases, causing the selection to be put in the wrong place and needlessly interrupting compositions. Fix leaking of resize event handlers. ## 0.19.44 (2022-02-17) ### Bug fixes Fix a crash that occasionally occurred when drag-selecting in a way that scrolled the editor. ### New features The new `EditorView.compositionStarted` property indicates whether a composition is starting. ## 0.19.43 (2022-02-16) ### Bug fixes Fix several issues where editing or composition went wrong due to our zero-width space kludge characters ending up in unexpected places. Make sure the editor re-measures its dimensions whenever its theme changes. Fix an issue where some keys on Android phones could leave the editor DOM unsynced with the actual document. ## 0.19.42 (2022-02-05) ### Bug fixes Fix a regression in cursor position determination after making an edit next to a widget. ## 0.19.41 (2022-02-04) ### Bug fixes Fix an issue where the editor's view of its content height could go out of sync with the DOM when a line-wrapping editor had its width changed, causing wrapping to change. Fix a bug that caused the editor to draw way too much content when scrolling to a position in an editor (much) taller than the window. Report an error when a replace decoration from a plugin crosses a line break, rather than silently ignoring it. Fix an issue where reading DOM changes was broken when `lineSeparator` contained more than one character. Make ordering of replace and mark decorations with the same extent and inclusivness more predictable by giving replace decorations precedence. Fix a bug where, on Chrome, replacement across line boundaries and next to widgets could cause bogus zero-width characters to appear in the content. ## 0.19.40 (2022-01-19) ### Bug fixes Make composition input properly appear at secondary cursors (except when those are in the DOM node with the composition, in which case the browser won't allow us to intervene without aborting the composition). Fix a bug that cause the editor to get confused about which content was visible after scrolling something into view. Fix a bug where the dummy elements rendered around widgets could end up in a separate set of wrapping marks, and thus become visible. `EditorView.moveVertically` now preserves the `assoc` property of the input range. Get rid of gaps between selection elements drawn by `drawSelection`. Fix an issue where replacing text next to a widget might leak bogus zero-width spaces into the document. Avoid browser selection mishandling when a focused view has `setState` called by eagerly refocusing it. ## 0.19.39 (2022-01-06) ### Bug fixes Make sure the editor signals a `geometryChanged` update when its width changes. ### New features `EditorView.darkTheme` can now be queried to figure out whether the editor is using a dark theme. ## 0.19.38 (2022-01-05) ### Bug fixes Fix a bug that caused line decorations with a `class` property to suppress all other line decorations for that line. Fix a bug that caused scroll effects to be corrupted when further updates came in before they were applied. Fix an issue where, depending on which way a floating point rounding error fell, `posAtCoords` (and thus vertical cursor motion) for positions outside of the vertical range of the document might or might not return the start/end of the document. ## 0.19.37 (2021-12-22) ### Bug fixes Fix regression where plugin replacing decorations that span to the end of the line are ignored. ## 0.19.36 (2021-12-22) ### Bug fixes Fix a crash in `posAtCoords` when the position lies in a block widget that is rendered but scrolled out of view. Adding block decorations from a plugin now raises an error. Replacing decorations that cross lines are ignored, when provided by a plugin. Fix inverted interpretation of the `precise` argument to `posAtCoords`. ## 0.19.35 (2021-12-20) ### Bug fixes The editor will now handle double-taps as if they are double-clicks, rather than letting the browser's native behavior happen (because the latter often does the wrong thing). Fix an issue where backspacing out a selection on Chrome Android would sometimes only delete the last character due to event order issues. `posAtCoords`, without second argument, will no longer return null for positions below or above the document. ## 0.19.34 (2021-12-17) ### Bug fixes Fix a bug where content line elements would in some cases lose their `cm-line` class. ## 0.19.33 (2021-12-16) ### Breaking changes `EditorView.scrollTo` and `EditorView.centerOn` are deprecated in favor of `EditorView.scrollIntoView`, and will be removed in the next breaking release. ### Bug fixes Fix an issue that could cause the editor to unnecessarily interfere with composition (especially visible on macOS Chrome). A composition started with multiple lines selected will no longer be interruptd by the editor. ### New features The new `EditorView.scrollIntoView` function allows you to do more fine-grained scrolling. ## 0.19.32 (2021-12-15) ### Bug fixes Fix a bug where CodeMirror's own event handers would run even after a user-supplied handler called `preventDefault` on an event. Properly draw selections when negative text-indent is used for soft wrapping. Fix an issue where `viewportLineBlocks` could hold inaccurate height information when the vertical scaling changed. Fixes drop cursor positioning when the document is scrolled. Force a content measure when the editor comes into view Fix a bug that could cause the editor to not measure its layout the first time it came into view. ## 0.19.31 (2021-12-13) ### New features The package now exports a `dropCursor` extension that draws a cursor at the current drop position when dragging content over the editor. ## 0.19.30 (2021-12-13) ### Bug fixes Refine Android key event handling to work properly in a GBoard corner case where pressing Enter fires a bunch of spurious deleteContentBackward events. Fix a crash in `drawSelection` for some kinds of selections. Prevent a possibility where some content updates causes duplicate text to remain in DOM. ### New features Support a `maxLength` option to `MatchDecorator` that allows user code to control how far it scans into hidden parts of viewport lines. ## 0.19.29 (2021-12-09) ### Bug fixes Fix a bug that could cause out-of-view editors to get a nonsensical viewport and fail to scroll into view when asked to. Fix a bug where would return 0 when clicking below the content if the last line was replaced with a block widget decoration. Fix an issue where clicking at the position of the previous cursor in a blurred editor would cause the selection to reset to the start of the document. Fix an issue where composition could be interrupted if the browser created a new node inside a mark decoration node. ## 0.19.28 (2021-12-08) ### Bug fixes Fix an issue where pressing Enter on Chrome Android during composition did not fire key handlers for Enter. Avoid a Chrome bug where the virtual keyboard closes when pressing backspace after a widget. Fix an issue where the editor could show a horizontal scroll bar even after all lines that caused it had been deleted or changed. ## 0.19.27 (2021-12-06) ### Bug fixes Fix a bug that could cause `EditorView.plugin` to inappropriately return `null` during plugin initialization. Fix a bug where a block widget without `estimatedHeight` at the end of the document could fail to be drawn ## 0.19.26 (2021-12-03) ### New features Widgets can now define a `destroy` method that is called when they are removed from the view. ## 0.19.25 (2021-12-02) ### Bug fixes Widgets around replaced ranges are now visible when their side does not point towards the replaced range. A replaced line with a line decoration no longer creates an extra empty line block in the editor. The `scrollPastEnd` extension will now over-reserve space at the bottom of the editor on startup, to prevent restored scroll positions from being clipped. ### New features `EditorView.editorAttributes` and `contentAttributes` may now hold functions that produce the attributes. ## 0.19.24 (2021-12-01) ### Bug fixes Fix a bug where `lineBlockAt`, for queries inside the viewport, would always return the first line in the viewport. ## 0.19.23 (2021-11-30) ### Bug fixes Fix an issue where after some kinds of changes, `EditorView.viewportLineBlocks` held an out-of-date set of blocks. ### New features Export `EditorView.documentPadding`, with information about the vertical padding of the document. ## 0.19.22 (2021-11-30) ### Bug fixes Fix an issue where editors with large vertical padding (for example via `scrollPastEnd`) could sometimes lose their scroll position on Chrome. Avoid some unnecessary DOM measuring work by more carefully checking whether it is needed. ### New features The new `elementAtHeight`, `lineBlockAtHeight`, and `lineBlockAt` methods provide a simpler and more efficient replacement for the (now deprecated) `blockAtHeight`, `visualLineAtHeight`, and `visualLineAt` methods. The editor view now exports a `documentTop` getter that gives you the vertical position of the top of the document. All height info is queried and reported relative to this top. The editor view's new `viewportLineBlocks` property provides an array of in-viewport line blocks, and replaces the (now deprecated) `viewportLines` method. ## 0.19.21 (2021-11-26) ### Bug fixes Fix a problem where the DOM update would unnecessarily trigger browser relayouts. ## 0.19.20 (2021-11-19) ### Bug fixes Run a measure cycle when the editor's size spontaneously changes. ## 0.19.19 (2021-11-17) ### Bug fixes Fix a bug that caused the precedence of `editorAttributes` and `contentAttributes` to be inverted, making lower-precedence extensions override higher-precedence ones. ## 0.19.18 (2021-11-16) ### Bug fixes Fix an issue where the editor wasn't aware it was line-wrapping with its own `lineWrapping` extension enabled. ## 0.19.17 (2021-11-16) ### Bug fixes Avoid an issue where stretches of whitespace on line wrap points could cause the cursor to be placed outside of the content. ## 0.19.16 (2021-11-11) ### Breaking changes Block replacement decorations now default to inclusive, because non-inclusive block decorations are rarely what you need. ### Bug fixes Fix an issue that caused block widgets to always have a large side value, making it impossible to show them between to replacement decorations. Fix a crash that could happen after some types of viewport changes, due to a bug in the block widget view data structure. ## 0.19.15 (2021-11-09) ### Bug fixes Fix a bug where the editor would think it was invisible when the document body was given screen height and scroll behavior. Fix selection reading inside a shadow root on iOS. ## 0.19.14 (2021-11-07) ### Bug fixes Fix an issue where typing into a read-only editor would move the selection. Fix slowness when backspace is held down on iOS. ## 0.19.13 (2021-11-06) ### Bug fixes Fix a bug where backspace, enter, and delete would get applied twice on iOS. ## 0.19.12 (2021-11-04) ### Bug fixes Make sure the workaround for the lost virtual keyboard on Chrome Android also works on slower phones. Slight style change in beforeinput handler Avoid failure cases in viewport-based rendering of very long lines. ## 0.19.11 (2021-11-03) ### Breaking changes `EditorView.scrollPosIntoView` has been deprecated. Use the `EditorView.scrollTo` effect instead. ### New features The new `EditorView.centerOn` effect can be used to scroll a given range to the center of the view. ## 0.19.10 (2021-11-02) ### Bug fixes Don't crash when `IntersectionObserver` fires its callback without any records. Try to handle some backspace issues on Chrome Android Using backspace near uneditable widgets on Chrome Android should now be more reliable. Work around a number of browser bugs by always rendering zero-width spaces around in-content widgets, so that browsers will treat the positions near them as valid cursor positions and not try to run composition across widget boundaries. Work around bogus composition changes created by Chrome Android after handled backspace presses. Work around an issue where tapping on an uneditable node in the editor would sometimes fail to show the virtual keyboard on Chrome Android. Prevent translation services from translating the editor content. Show direction override characters as special chars by default `specialChars` will now, by default, replace direction override chars, to mitigate https://trojansource.codes/ attacks. ### New features The editor view will, if `parent` is given but `root` is not, derive the root from the parent element. Line decorations now accept a `class` property to directly add DOM classes to the line. ## 0.19.9 (2021-10-01) ### Bug fixes Fix an issue where some kinds of reflows in the surrounding document could move unrendered parts of the editor into view without the editor noticing and updating its viewport. Fix an occasional crash in the selection drawing extension. ## 0.19.8 (2021-09-26) ### Bug fixes Fix a bug that could, on DOM changes near block widgets, insert superfluous line breaks. Make interacting with a destroyed editor view do nothing, rather than crash, to avoid tripping people up with pending timeouts and such. Make sure `ViewUpdate.viewportChanged` is true whenever `visibleRanges` changes, so that plugins acting only on visible ranges can use it to check when to update. Fix line-wise cut on empty lines. ## 0.19.7 (2021-09-13) ### Bug fixes The view is now aware of the new `EditorState.readOnly` property, and suppresses events that modify the document when it is true. ## 0.19.6 (2021-09-10) ### Bug fixes Remove a `console.log` that slipped into the previous release. ## 0.19.5 (2021-09-09) ### New features The new `EditorView.scrollTo` effect can be used to scroll a given range into view. ## 0.19.4 (2021-09-01) ### Bug fixes Fix an issue where lines containing just a widget decoration wrapped in a mark decoration could be displayed with 0 height. ## 0.19.3 (2021-08-25) ### Bug fixes Fix a view corruption that could happen in situations involving overlapping mark decorations. ## 0.19.2 (2021-08-23) ### New features The package now exports a `scrollPastEnd` function, which returns an extension that adds space below the document to allow the last line to be scrolled to the top of the editor. ## 0.19.1 (2021-08-11) ### Breaking changes The view now emits new-style user event annotations for the transactions it generates. ### Bug fixes Fix a bug where `coordsAtPos` would allow the passed `side` argument to override widget sides, producing incorrect cursor positions. Fix a bug that could cause content lines to be misaligned in certain situations involving widgets at the end of lines. Fix an issue where, if the browser decided to modify DOM attributes in the content in response to some editing action, the view failed to reset those again. ## 0.18.19 (2021-07-12) ### Bug fixes Fix a regression where `EditorView.editable.of(false)` didn't disable editing on Webkit-based browsers. ## 0.18.18 (2021-07-06) ### Bug fixes Fix a bug that caused `EditorView.moveVertically` to only move by one line, even when given a custom distance, in some cases. Hide Safari's native bold/italic/underline controls for the content. Fix a CSS problem that prevented Safari from breaking words longer than the line in line-wrapping mode. Avoid a problem where composition would be inappropriately abored on Safari. Fix drag-selection that scrolls the content by dragging past the visible viewport. ### New features `posAtCoords` now has an imprecise mode where it'll return an approximate position even for parts of the document that aren't currently rendered. ## 0.18.17 (2021-06-14) ### Bug fixes Make `drawSelection` behave properly when lines are split by block widgets. Make sure drawn selections that span a single line break don't leave a gap between the lines. ## 0.18.16 (2021-06-03) ### Bug fixes Fix a crash that could occur when the document changed during mouse selection. Fix a bug where composition inside styled content would sometimes be inappropriately aborted by editor DOM updates. ### New features `MouseSelectionStyle.update` may now return true to indicate it should be queried for a new selection after the update. ## 0.18.15 (2021-06-01) ### Bug fixes Fix a bug that would, in very specific circumstances, cause `posAtCoords` to go into an infinite loop in Safari. Fix a bug where some types of IME input on Mobile Safari would drop text. ## 0.18.14 (2021-05-28) ### Bug fixes Fix an issue where the DOM selection was sometimes not properly updated when next to a widget. Invert the order in which overlapping decorations are drawn so that higher-precedence decorations are nested inside lower-precedence ones (and thus override their styling). Fix a but in `posAtCoords` where it would in some situations return -1 instead of `null`. ### New features A new plugin field, `PluginField.atomicRanges`, can be used to cause cursor motion to skip past some ranges of the document. ## 0.18.13 (2021-05-20) ### Bug fixes Fix a bug that would cause the content DOM update to crash in specific circumstances. Work around an issue where, after some types of changes, Mobile Safari would ignore Enter presses. Make iOS enter and backspace handling more robust, so that platform bugs are less likely to break those keys in the editor. Fix a regression where Esc + Tab no longer allowed the user to exit the editor. ### New features You can now drop text files into the editor. ## 0.18.12 (2021-05-10) ### Bug fixes Work around a Mobile Safari bug where, after backspacing out the last character on a line, Enter didn't work anymore. Work around a problem in Mobile Safari where you couldn't tap to put the cursor at the end of a line that ended in a widget. ## 0.18.11 (2021-04-30) ### Bug fixes Add an attribute to prevent the Grammarly browser extension from messing with the editor content. Fix more issues around selection handling a Shadow DOM in Safari. ## 0.18.10 (2021-04-27) ### Bug fixes Fix a bug where some types of updates wouldn't properly cause marks around the changes to be joined in the DOM. Fix an issue where the content and gutters in a fixed-height editor could be smaller than the editor height. Fix a crash on Safari when initializing an editor in an unfocused window. Fix a bug where the editor would incorrectly conclude it was out of view in some types of absolutely positioned parent elements. ## 0.18.9 (2021-04-23) ### Bug fixes Fix a crash that occurred when determining DOM coordinates in some specific situations. Fix a crash when a DOM change that ended at a zero-width view element (widget) removed that element from the DOM. Disable autocorrect and autocapitalize by default, since in most code-editor contexts they get in the way. You can use `EditorView.contentAttributes` to override this. Fix a bug that interfered with native touch selection handling on Android. Fix an unnecessary DOM update after composition that would disrupt touch selection on Android. Add a workaround for Safari's broken selection reporting when the editor is in a shadow DOM tree. Fix select-all from the context menu on Safari. ## 0.18.8 (2021-04-19) ### Bug fixes Handle selection replacements where the inserted text matches the start/end of the replaced text better. Fix an issue where the editor would miss scroll events when it was placed in a DOM component slot. ## 0.18.7 (2021-04-13) ### Bug fixes Fix a crash when drag-selecting out of the editor with editable turned off. Backspace and delete now largely work in an editor without a keymap. Pressing backspace on iOS should now properly update the virtual keyboard's capitalize and autocorrect state. Prevent random line-wrapping in (non-wrapping) editors on Mobile Safari. ## 0.18.6 (2021-04-08) ### Bug fixes Fix an issue in the compiled output that would break the code when minified with terser. ## 0.18.5 (2021-04-07) ### Bug fixes Improve handling of bidi text with brackets (conforming to Unicode 13's bidi algorithm). Fix the position where `drawSelection` displays the cursor on bidi boundaries. ## 0.18.4 (2021-04-07) ### Bug fixes Fix an issue where the default focus ring gets obscured by the gutters and active line. Fix an issue where the editor believed Chrome Android didn't support the CSS `tab-size` style. Don't style active lines when there are non-empty selection ranges, so that the active line background doesn't obscure the selection. Make iOS autocapitalize update properly when you press Enter. ## 0.18.3 (2021-03-19) ### Breaking changes The outer DOM element now has class `cm-editor` instead of `cm-wrap` (`cm-wrap` will be present as well until 0.19). ### Bug fixes Improve behavior of `posAtCoords` when the position is near text but not in any character's actual box. ## 0.18.2 (2021-03-19) ### Bug fixes Triple-clicking now selects the line break after the clicked line (if any). Fix an issue where the `drawSelection` plugin would fail to draw the top line of the selection when it started in an empty line. Fix an issue where, at the end of a specific type of composition on iOS, the editor read the DOM before the browser was done updating it. ## 0.18.1 (2021-03-05) ### Bug fixes Fix an issue where, on iOS, some types of IME would cause the composed content to be deleted when confirming a composition. ## 0.18.0 (2021-03-03) ### Breaking changes The `themeClass` function and ``-style selectors in themes are no longer supported (prefixing with `cm-` should be done manually now). Themes must now use `&` (instead of an extra `$`) to target the editor wrapper element. The editor no longer adds `cm-light` or `cm-dark` classes. Targeting light or dark configurations in base themes should now be done by using a `&light` or `&dark` top-level selector. ## 0.17.13 (2021-03-03) ### Bug fixes Work around a Firefox bug where it won't draw the cursor when it is between uneditable elements. Fix a bug that broke built-in mouse event handling. ## 0.17.12 (2021-03-02) ### Bug fixes Avoid interfering with touch events, to allow native selection behavior. Fix a bug that broke sub-selectors with multiple `&` placeholders in themes. ## 0.17.11 (2021-02-25) ### Bug fixes Fix vertical cursor motion on Safari with a larger line-height. Fix incorrect selection drawing (with `drawSelection`) when the selection spans to just after a soft wrap point. Fix an issue where compositions on Safari were inappropriately aborted in some circumstances. The view will now redraw when the `EditorView.phrases` facet changes, to make sure translated text is properly updated. ## 0.17.10 (2021-02-22) ### Bug fixes Long words without spaces, when line-wrapping is enabled, are now properly broken. Fix the horizontal position of selections drawn by `drawSelection` in right-to-left editors with a scrollbar. ## 0.17.9 (2021-02-18) ### Bug fixes Fix an issue where pasting linewise at the start of a line left the cursor before the inserted content. ## 0.17.8 (2021-02-16) ### Bug fixes Fix a problem where the DOM selection and the editor state could get out of sync in non-editable mode. Fix a crash when the editor was hidden on Safari, due to `getClientRects` returning an empty list. Prevent Firefox from making the scrollable element keyboard-focusable. ## 0.17.7 (2021-01-25) ### New features Add an `EditorView.announce` state effect that can be used to conveniently provide screen reader announcements. ## 0.17.6 (2021-01-22) ### Bug fixes Avoid creating very high scroll containers for large documents so that we don't overflow the DOM's fixed-precision numbers. ## 0.17.5 (2021-01-15) ### Bug fixes Fix a bug that would create space-filling placeholders with incorrect height when document is very large. ## 0.17.4 (2021-01-14) ### Bug fixes The `drawSelection` extension will now reuse cursor DOM nodes when the number of cursors stays the same, allowing some degree of cursor transition animation. Makes highlighted special characters styleable (``) and fix their default look in dark themes to have appropriate contrast. ### New features Adds a new `MatchDecorator` helper class which can be used to easily maintain decorations on content that matches a regular expression. ## 0.17.3 (2021-01-06) ### New features The package now also exports a CommonJS module. ## 0.17.2 (2021-01-04) ### Bug fixes Work around Chrome problem where the native shift-enter behavior inserts two line breaks. Make bracket closing and bracket pair removing more reliable on Android. Fix bad cursor position and superfluous change transactions after pressing enter when in a composition on Android. Fix issue where the wrong character was deleted when backspacing out a character before an identical copy of that character on Android. ## 0.17.1 (2020-12-30) ### Bug fixes Fix a bug that prevented `ViewUpdate.focusChanged` from ever being true. ## 0.17.0 (2020-12-29) ### Breaking changes First numbered release. view-6.38.1/LICENSE000066400000000000000000000021361503537325200135770ustar00rootroot00000000000000MIT License Copyright (C) 2018-2021 by Marijn Haverbeke and others Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. view-6.38.1/README.md000066400000000000000000000027141503537325200140530ustar00rootroot00000000000000# @codemirror/view [![NPM version](https://img.shields.io/npm/v/@codemirror/view.svg)](https://www.npmjs.org/package/@codemirror/view) [ [**WEBSITE**](https://codemirror.net/) | [**DOCS**](https://codemirror.net/docs/ref/#view) | [**ISSUES**](https://github.com/codemirror/dev/issues) | [**FORUM**](https://discuss.codemirror.net/c/next/) | [**CHANGELOG**](https://github.com/codemirror/view/blob/main/CHANGELOG.md) ] This package implements the DOM view component for the [CodeMirror](https://codemirror.net/) code editor. The [project page](https://codemirror.net/) has more information, a number of [examples](https://codemirror.net/examples/) and the [documentation](https://codemirror.net/docs/). This code is released under an [MIT license](https://github.com/codemirror/view/tree/main/LICENSE). We aim to be an inclusive, welcoming community. To make that explicit, we have a [code of conduct](http://contributor-covenant.org/version/1/1/0/) that applies to communication around the project. ## Usage ```javascript import {EditorView} from "@codemirror/view" import {basicSetup} from "codemirror" const view = new EditorView({ parent: document.querySelector("#some-node"), doc: "Content text", extensions: [basicSetup /* ... */] }) ``` Add additional extensions, such as a [language mode](https://codemirror.net/#languages), to configure the editor. Call [`view.dispatch`](https://codemirror.net/docs/ref/#view.EditorView.dispatch) to update the editor's state. view-6.38.1/package.json000066400000000000000000000016441503537325200150630ustar00rootroot00000000000000{ "name": "@codemirror/view", "version": "6.38.1", "description": "DOM view component for the CodeMirror code editor", "scripts": { "test": "cm-runtests", "prepare": "cm-buildhelper src/index.ts" }, "keywords": [ "editor", "code" ], "author": { "name": "Marijn Haverbeke", "email": "marijn@haverbeke.berlin", "url": "http://marijnhaverbeke.nl" }, "type": "module", "main": "dist/index.cjs", "exports": { "import": "./dist/index.js", "require": "./dist/index.cjs" }, "types": "dist/index.d.ts", "module": "dist/index.js", "sideEffects": false, "license": "MIT", "dependencies": { "@codemirror/state": "^6.5.0", "crelt": "^1.0.6", "style-mod": "^4.1.0", "w3c-keyname": "^2.2.4" }, "devDependencies": { "@codemirror/buildhelper": "^1.0.0" }, "repository": { "type": "git", "url": "https://github.com/codemirror/view.git" } } view-6.38.1/src/000077500000000000000000000000001503537325200133575ustar00rootroot00000000000000view-6.38.1/src/README.md000066400000000000000000000050551503537325200146430ustar00rootroot00000000000000The “view” is the part of the editor that the user sees—a DOM component that displays the editor state and allows text input. @EditorViewConfig @EditorView @Direction @BlockInfo @BlockType @BidiSpan @DOMEventHandlers @DOMEventMap @Rect ### Extending the View @Command @ViewPlugin @PluginValue @PluginSpec @ViewUpdate @logException @MouseSelectionStyle @drawSelection @getDrawSelectionConfig @dropCursor @highlightActiveLine @highlightSpecialChars @highlightWhitespace @highlightTrailingWhitespace @placeholder @scrollPastEnd ### Key bindings @KeyBinding @keymap @runScopeHandlers ### Decorations Your code should not try to directly change the DOM structure CodeMirror creates for its content—that will not work. Instead, the way to influence how things are drawn is by providing decorations, which can add styling or replace content with an alternative representation. @Decoration @DecorationSet @WidgetType @MatchDecorator ### Gutters Functionality for showing "gutters" (for line numbers or other purposes) on the side of the editor. See also the [gutter example](../../examples/gutter/). @lineNumbers @highlightActiveLineGutter @gutter @gutters @GutterMarker @gutterLineClass @gutterWidgetClass @lineNumberMarkers @lineNumberWidgetMarker ### Tooltips Tooltips are DOM elements overlaid on the editor near a given document position. This package helps manage and position such elements. See also the [tooltip example](../../examples/tooltip/). @showTooltip @Tooltip @TooltipView @tooltips @getTooltip @hoverTooltip @HoverTooltipSource @hasHoverTooltips @closeHoverTooltips @repositionTooltips ### Panels Panels are UI elements positioned above or below the editor (things like a search dialog). They will take space from the editor when it has a fixed height, and will stay in view even when the editor is partially scrolled out of view. See also the [panel example](../../examples/panel/). @showPanel @PanelConstructor @Panel @getPanel @panels @showDialog @getDialog ### Layers Layers are sets of DOM elements drawn over or below the document text. They can be useful for displaying user interface elements that don't take up space and shouldn't influence line wrapping, such as additional cursors. Note that, being outside of the regular DOM order, such elements are invisible to screen readers. Make sure to also [provide](#view.EditorView^announce) any important information they convey in an accessible way. @layer @LayerMarker @RectangleMarker ### Rectangular Selection @rectangularSelection @crosshairCursor view-6.38.1/src/active-line.ts000066400000000000000000000021021503537325200161220ustar00rootroot00000000000000import {Extension} from "@codemirror/state" import {EditorView} from "./editorview" import {ViewPlugin, ViewUpdate} from "./extension" import {Decoration, DecorationSet} from "./decoration" /// Mark lines that have a cursor on them with the `"cm-activeLine"` /// DOM class. export function highlightActiveLine(): Extension { return activeLineHighlighter } const lineDeco = Decoration.line({class: "cm-activeLine"}) const activeLineHighlighter = ViewPlugin.fromClass(class { decorations: DecorationSet constructor(view: EditorView) { this.decorations = this.getDeco(view) } update(update: ViewUpdate) { if (update.docChanged || update.selectionSet) this.decorations = this.getDeco(update.view) } getDeco(view: EditorView) { let lastLineStart = -1, deco = [] for (let r of view.state.selection.ranges) { let line = view.lineBlockAt(r.head) if (line.from > lastLineStart) { deco.push(lineDeco.range(line.from)) lastLineStart = line.from } } return Decoration.set(deco) } }, { decorations: v => v.decorations }) view-6.38.1/src/attributes.ts000066400000000000000000000031251503537325200161160ustar00rootroot00000000000000export type Attrs = {[name: string]: string} export function combineAttrs(source: Attrs, target: Attrs) { for (let name in source) { if (name == "class" && target.class) target.class += " " + source.class else if (name == "style" && target.style) target.style += ";" + source.style else target[name] = source[name] } return target } const noAttrs = Object.create(null) export function attrsEq(a: Attrs | null, b: Attrs | null, ignore?: string): boolean { if (a == b) return true if (!a) a = noAttrs if (!b) b = noAttrs let keysA = Object.keys(a!), keysB = Object.keys(b!) if (keysA.length - (ignore && keysA.indexOf(ignore) > -1 ? 1 : 0) != keysB.length - (ignore && keysB.indexOf(ignore) > -1 ? 1 : 0)) return false for (let key of keysA) { if (key != ignore && (keysB.indexOf(key) == -1 || a![key] !== b![key])) return false } return true } export function updateAttrs(dom: HTMLElement, prev: Attrs | null, attrs: Attrs | null) { let changed = false if (prev) for (let name in prev) if (!(attrs && name in attrs)) { changed = true if (name == "style") dom.style.cssText = "" else dom.removeAttribute(name) } if (attrs) for (let name in attrs) if (!(prev && prev[name] == attrs[name])) { changed = true if (name == "style") dom.style.cssText = attrs[name] else dom.setAttribute(name, attrs[name]) } return changed } export function getAttrs(dom: HTMLElement) { let attrs = Object.create(null) for (let i = 0; i < dom.attributes.length; i++) { let attr = dom.attributes[i] attrs[attr.name] = attr.value } return attrs } view-6.38.1/src/bidi.ts000066400000000000000000000451431503537325200146450ustar00rootroot00000000000000import {EditorSelection, SelectionRange, Line, findClusterBreak} from "@codemirror/state" /// Used to indicate [text direction](#view.EditorView.textDirection). export enum Direction { // (These are chosen to match the base levels, in bidi algorithm // terms, of spans in that direction.) /// Left-to-right. LTR = 0, /// Right-to-left. RTL = 1 } const LTR = Direction.LTR, RTL = Direction.RTL // Codes used for character types: const enum T { L = 1, // Left-to-Right R = 2, // Right-to-Left AL = 4, // Right-to-Left Arabic EN = 8, // European Number AN = 16, // Arabic Number ET = 64, // European Number Terminator CS = 128, // Common Number Separator NI = 256, // Neutral or Isolate (BN, N, WS), NSM = 512, // Non-spacing Mark Strong = T.L | T.R | T.AL, Num = T.EN | T.AN } // Decode a string with each type encoded as log2(type) function dec(str: string): readonly T[] { let result = [] for (let i = 0; i < str.length; i++) result.push(1 << +str[i]) return result } // Character types for codepoints 0 to 0xf8 const LowTypes = dec("88888888888888888888888888888888888666888888787833333333337888888000000000000000000000000008888880000000000000000000000000088888888888888888888888888888888888887866668888088888663380888308888800000000000000000000000800000000000000000000000000000008") // Character types for codepoints 0x600 to 0x6f9 const ArabicTypes = dec("4444448826627288999999999992222222222222222222222222222222222222222222222229999999999999999999994444444444644222822222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222999999949999999229989999223333333333") const Brackets = Object.create(null), BracketStack: number[] = [] // There's a lot more in // https://www.unicode.org/Public/UCD/latest/ucd/BidiBrackets.txt, // which are left out to keep code size down. for (let p of ["()", "[]", "{}"]) { let l = p.charCodeAt(0), r = p.charCodeAt(1) Brackets[l] = r; Brackets[r] = -l } // Tracks direction in and before bracketed ranges. const enum Bracketed { OppositeBefore = 1, EmbedInside = 2, OppositeInside = 4, MaxDepth = 3 * 63 } function charType(ch: number) { return ch <= 0xf7 ? LowTypes[ch] : 0x590 <= ch && ch <= 0x5f4 ? T.R : 0x600 <= ch && ch <= 0x6f9 ? ArabicTypes[ch - 0x600] : 0x6ee <= ch && ch <= 0x8ac ? T.AL : 0x2000 <= ch && ch <= 0x200c ? T.NI : 0xfb50 <= ch && ch <= 0xfdff ? T.AL : T.L } const BidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac\ufb50-\ufdff]/ /// Represents a contiguous range of text that has a single direction /// (as in left-to-right or right-to-left). export class BidiSpan { /// The direction of this span. get dir(): Direction { return this.level % 2 ? RTL : LTR } /// @internal constructor( /// The start of the span (relative to the start of the line). readonly from: number, /// The end of the span. readonly to: number, /// The ["bidi /// level"](https://unicode.org/reports/tr9/#Basic_Display_Algorithm) /// of the span (in this context, 0 means /// left-to-right, 1 means right-to-left, 2 means left-to-right /// number inside right-to-left text). readonly level: number ) {} /// @internal side(end: boolean, dir: Direction) { return (this.dir == dir) == end ? this.to : this.from } /// @internal forward(forward: boolean, dir: Direction) { return forward == (this.dir == dir) } /// @internal static find(order: readonly BidiSpan[], index: number, level: number, assoc: number) { let maybe = -1 for (let i = 0; i < order.length; i++) { let span = order[i] if (span.from <= index && span.to >= index) { if (span.level == level) return i // When multiple spans match, if assoc != 0, take the one that // covers that side, otherwise take the one with the minimum // level. if (maybe < 0 || (assoc != 0 ? (assoc < 0 ? span.from < index : span.to > index) : order[maybe].level > span.level)) maybe = i } } if (maybe < 0) throw new RangeError("Index out of range") return maybe } } // Arrays of isolates are always sorted by position. Isolates are // never empty. Nested isolates don't stick out of their parent. export type Isolate = {from: number, to: number, direction: Direction, inner: readonly Isolate[]} export function isolatesEq(a: readonly Isolate[], b: readonly Isolate[]) { if (a.length != b.length) return false for (let i = 0; i < a.length; i++) { let iA = a[i], iB = b[i] if (iA.from != iB.from || iA.to != iB.to || iA.direction != iB.direction || !isolatesEq(iA.inner, iB.inner)) return false } return true } // Reused array of character types const types: T[] = [] // Fill in the character types (in `types`) from `from` to `to` and // apply W normalization rules. function computeCharTypes(line: string, rFrom: number, rTo: number, isolates: readonly Isolate[], outerType: T) { for (let iI = 0; iI <= isolates.length; iI++) { let from = iI ? isolates[iI - 1].to : rFrom, to = iI < isolates.length ? isolates[iI].from : rTo let prevType = iI ? T.NI : outerType // W1. Examine each non-spacing mark (NSM) in the level run, and // change the type of the NSM to the type of the previous // character. If the NSM is at the start of the level run, it will // get the type of sor. // W2. Search backwards from each instance of a European number // until the first strong type (R, L, AL, or sor) is found. If an // AL is found, change the type of the European number to Arabic // number. // W3. Change all ALs to R. // (Left after this: L, R, EN, AN, ET, CS, NI) for (let i = from, prev = prevType, prevStrong = prevType; i < to; i++) { let type = charType(line.charCodeAt(i)) if (type == T.NSM) type = prev else if (type == T.EN && prevStrong == T.AL) type = T.AN types[i] = type == T.AL ? T.R : type if (type & T.Strong) prevStrong = type prev = type } // W5. A sequence of European terminators adjacent to European // numbers changes to all European numbers. // W6. Otherwise, separators and terminators change to Other // Neutral. // W7. Search backwards from each instance of a European number // until the first strong type (R, L, or sor) is found. If an L is // found, then change the type of the European number to L. // (Left after this: L, R, EN+AN, NI) for (let i = from, prev = prevType, prevStrong = prevType; i < to; i++) { let type = types[i] if (type == T.CS) { if (i < to - 1 && prev == types[i + 1] && (prev & T.Num)) type = types[i] = prev else types[i] = T.NI } else if (type == T.ET) { let end = i + 1 while (end < to && types[end] == T.ET) end++ let replace = (i && prev == T.EN) || (end < rTo && types[end] == T.EN) ? (prevStrong == T.L ? T.L : T.EN) : T.NI for (let j = i; j < end; j++) types[j] = replace i = end - 1 } else if (type == T.EN && prevStrong == T.L) { types[i] = T.L } prev = type if (type & T.Strong) prevStrong = type } } } // Process brackets throughout a run sequence. function processBracketPairs(line: string, rFrom: number, rTo: number, isolates: readonly Isolate[], outerType: T) { let oppositeType = outerType == T.L ? T.R : T.L for (let iI = 0, sI = 0, context = 0; iI <= isolates.length; iI++) { let from = iI ? isolates[iI - 1].to : rFrom, to = iI < isolates.length ? isolates[iI].from : rTo // N0. Process bracket pairs in an isolating run sequence // sequentially in the logical order of the text positions of the // opening paired brackets using the logic given below. Within this // scope, bidirectional types EN and AN are treated as R. for (let i = from, ch, br, type; i < to; i++) { // Keeps [startIndex, type, strongSeen] triples for each open // bracket on BracketStack. if (br = Brackets[ch = line.charCodeAt(i)]) { if (br < 0) { // Closing bracket for (let sJ = sI - 3; sJ >= 0; sJ -= 3) { if (BracketStack[sJ + 1] == -br) { let flags = BracketStack[sJ + 2] let type = (flags & Bracketed.EmbedInside) ? outerType : !(flags & Bracketed.OppositeInside) ? 0 : (flags & Bracketed.OppositeBefore) ? oppositeType : outerType if (type) types[i] = types[BracketStack[sJ]] = type sI = sJ break } } } else if (BracketStack.length == Bracketed.MaxDepth) { break } else { BracketStack[sI++] = i BracketStack[sI++] = ch BracketStack[sI++] = context } } else if ((type = types[i]) == T.R || type == T.L) { let embed = type == outerType context = embed ? 0 : Bracketed.OppositeBefore for (let sJ = sI - 3; sJ >= 0; sJ -= 3) { let cur = BracketStack[sJ + 2] if (cur & Bracketed.EmbedInside) break if (embed) { BracketStack[sJ + 2] |= Bracketed.EmbedInside } else { if (cur & Bracketed.OppositeInside) break BracketStack[sJ + 2] |= Bracketed.OppositeInside } } } } } } function processNeutrals(rFrom: number, rTo: number, isolates: readonly Isolate[], outerType: T) { for (let iI = 0, prev = outerType; iI <= isolates.length; iI++) { let from = iI ? isolates[iI - 1].to : rFrom, to = iI < isolates.length ? isolates[iI].from : rTo // N1. A sequence of neutrals takes the direction of the // surrounding strong text if the text on both sides has the same // direction. European and Arabic numbers act as if they were R in // terms of their influence on neutrals. Start-of-level-run (sor) // and end-of-level-run (eor) are used at level run boundaries. // N2. Any remaining neutrals take the embedding direction. // (Left after this: L, R, EN+AN) for (let i = from; i < to;) { let type = types[i] if (type == T.NI) { let end = i + 1 for (;;) { if (end == to) { if (iI == isolates.length) break end = isolates[iI++].to to = iI < isolates.length ? isolates[iI].from : rTo } else if (types[end] == T.NI) { end++ } else { break } } let beforeL = prev == T.L let afterL = (end < rTo ? types[end] : outerType) == T.L let replace = beforeL == afterL ? (beforeL ? T.L : T.R) : outerType for (let j = end, jI = iI, fromJ = jI ? isolates[jI - 1].to : rFrom; j > i;) { if (j == fromJ) { j = isolates[--jI].from; fromJ = jI ? isolates[jI - 1].to : rFrom } types[--j] = replace } i = end } else { prev = type i++ } } } } // Find the contiguous ranges of character types in a given range, and // emit spans for them. Flip the order of the spans as appropriate // based on the level, and call through to compute the spans for // isolates at the proper point. function emitSpans(line: string, from: number, to: number, level: number, baseLevel: number, isolates: readonly Isolate[], order: BidiSpan[]) { let ourType = level % 2 ? T.R : T.L if ((level % 2) == (baseLevel % 2)) { // Same dir as base direction, don't flip for (let iCh = from, iI = 0; iCh < to;) { // Scan a section of characters in direction ourType, unless // there's another type of char right after iCh, in which case // we scan a section of other characters (which, if ourType == // T.L, may contain both T.R and T.AN chars). let sameDir = true, isNum = false if (iI == isolates.length || iCh < isolates[iI].from) { let next = types[iCh] if (next != ourType) { sameDir = false; isNum = next == T.AN } } // Holds an array of isolates to pass to a recursive call if we // must recurse (to distinguish T.AN inside an RTL section in // LTR text), null if we can emit directly let recurse: Isolate[] | null = !sameDir && ourType == T.L ? [] : null let localLevel = sameDir ? level : level + 1 let iScan = iCh run: for (;;) { if (iI < isolates.length && iScan == isolates[iI].from) { if (isNum) break run let iso = isolates[iI] // Scan ahead to verify that there is another char in this dir after the isolate(s) if (!sameDir) for (let upto = iso.to, jI = iI + 1;;) { if (upto == to) break run if (jI < isolates.length && isolates[jI].from == upto) upto = isolates[jI++].to else if (types[upto] == ourType) break run else break } iI++ if (recurse) { recurse.push(iso) } else { if (iso.from > iCh) order.push(new BidiSpan(iCh, iso.from, localLevel)) let dirSwap = (iso.direction == LTR) != !(localLevel % 2) computeSectionOrder(line, dirSwap ? level + 1 : level, baseLevel, iso.inner, iso.from, iso.to, order) iCh = iso.to } iScan = iso.to } else if (iScan == to || (sameDir ? types[iScan] != ourType : types[iScan] == ourType)) { break } else { iScan++ } } if (recurse) emitSpans(line, iCh, iScan, level + 1, baseLevel, recurse, order) else if (iCh < iScan) order.push(new BidiSpan(iCh, iScan, localLevel)) iCh = iScan } } else { // Iterate in reverse to flip the span order. Same code again, but // going from the back of the section to the front for (let iCh = to, iI = isolates.length; iCh > from;) { let sameDir = true, isNum = false if (!iI || iCh > isolates[iI - 1].to) { let next = types[iCh - 1] if (next != ourType) { sameDir = false; isNum = next == T.AN } } let recurse: Isolate[] | null = !sameDir && ourType == T.L ? [] : null let localLevel = sameDir ? level : level + 1 let iScan = iCh run: for (;;) { if (iI && iScan == isolates[iI - 1].to) { if (isNum) break run let iso = isolates[--iI] // Scan ahead to verify that there is another char in this dir after the isolate(s) if (!sameDir) for (let upto = iso.from, jI = iI;;) { if (upto == from) break run if (jI && isolates[jI - 1].to == upto) upto = isolates[--jI].from else if (types[upto - 1] == ourType) break run else break } if (recurse) { recurse.push(iso) } else { if (iso.to < iCh) order.push(new BidiSpan(iso.to, iCh, localLevel)) let dirSwap = (iso.direction == LTR) != !(localLevel % 2) computeSectionOrder(line, dirSwap ? level + 1 : level, baseLevel, iso.inner, iso.from, iso.to, order) iCh = iso.from } iScan = iso.from } else if (iScan == from || (sameDir ? types[iScan - 1] != ourType : types[iScan - 1] == ourType)) { break } else { iScan-- } } if (recurse) emitSpans(line, iScan, iCh, level + 1, baseLevel, recurse, order) else if (iScan < iCh) order.push(new BidiSpan(iScan, iCh, localLevel)) iCh = iScan } } } function computeSectionOrder(line: string, level: number, baseLevel: number, isolates: readonly Isolate[], from: number, to: number, order: BidiSpan[]) { let outerType = (level % 2 ? T.R : T.L) as T computeCharTypes(line, from, to, isolates, outerType) processBracketPairs(line, from, to, isolates, outerType) processNeutrals(from, to, isolates, outerType) emitSpans(line, from, to, level, baseLevel, isolates, order) } export function computeOrder(line: string, direction: Direction, isolates: readonly Isolate[]) { if (!line) return [new BidiSpan(0, 0, direction == RTL ? 1 : 0)] if (direction == LTR && !isolates.length && !BidiRE.test(line)) return trivialOrder(line.length) if (isolates.length) while (line.length > types.length) types[types.length] = T.NI // Make sure types array has no gaps let order: BidiSpan[] = [], level = direction == LTR ? 0 : 1 computeSectionOrder(line, level, level, isolates, 0, line.length, order) return order } export function trivialOrder(length: number) { return [new BidiSpan(0, length, 0)] } export let movedOver = "" // This implementation moves strictly visually, without concern for a // traversal visiting every logical position in the string. It will // still do so for simple input, but situations like multiple isolates // with the same level next to each other, or text going against the // main dir at the end of the line, will make some positions // unreachable with this motion. Each visible cursor position will // correspond to the lower-level bidi span that touches it. // // The alternative would be to solve an order globally for a given // line, making sure that it includes every position, but that would // require associating non-canonical (higher bidi span level) // positions with a given visual position, which is likely to confuse // people. (And would generally be a lot more complicated.) export function moveVisually(line: Line, order: readonly BidiSpan[], dir: Direction, start: SelectionRange, forward: boolean) { let startIndex = start.head - line.from let spanI = BidiSpan.find(order, startIndex, start.bidiLevel ?? -1, start.assoc) let span = order[spanI], spanEnd = span.side(forward, dir) // End of span if (startIndex == spanEnd) { let nextI = spanI += forward ? 1 : -1 if (nextI < 0 || nextI >= order.length) return null span = order[spanI = nextI] startIndex = span.side(!forward, dir) spanEnd = span.side(forward, dir) } let nextIndex = findClusterBreak(line.text, startIndex, span.forward(forward, dir)) if (nextIndex < span.from || nextIndex > span.to) nextIndex = spanEnd movedOver = line.text.slice(Math.min(startIndex, nextIndex), Math.max(startIndex, nextIndex)) let nextSpan = spanI == (forward ? order.length - 1 : 0) ? null : order[spanI + (forward ? 1 : -1)] if (nextSpan && nextIndex == spanEnd && nextSpan.level + (forward ? 0 : 1) < span.level) return EditorSelection.cursor(nextSpan.side(!forward, dir) + line.from, nextSpan.forward(forward, dir) ? 1 : -1, nextSpan.level) return EditorSelection.cursor(nextIndex + line.from, span.forward(forward, dir) ? -1 : 1, span.level) } export function autoDirection(text: string, from: number, to: number) { for (let i = from; i < to; i++) { let type = charType(text.charCodeAt(i)) if (type == T.L) return LTR if (type == T.R || type == T.AL) return RTL } return LTR } view-6.38.1/src/blockview.ts000066400000000000000000000221411503537325200157140ustar00rootroot00000000000000import {ContentView, DOMPos, ViewFlag, noChildren, mergeChildrenInto} from "./contentview" import {DocView} from "./docview" import {TextView, MarkView, inlineDOMAtPos, joinInlineInto, coordsInChildren} from "./inlineview" import {clientRectsFor, Rect, flattenRect, clearAttributes} from "./dom" import {LineDecoration, WidgetType, PointDecoration} from "./decoration" import {Attrs, combineAttrs, attrsEq, updateAttrs} from "./attributes" import browser from "./browser" import {EditorView} from "./editorview" import {Text} from "@codemirror/state" export interface BlockView extends ContentView { covers(side: -1 | 1): boolean dom: HTMLElement | null } export class LineView extends ContentView implements BlockView { children: ContentView[] = [] length: number = 0 declare dom: HTMLElement | null prevAttrs: Attrs | null | undefined = undefined attrs: Attrs | null = null breakAfter = 0 declare parent: DocView | null // Consumes source merge(from: number, to: number, source: BlockView | null, hasStart: boolean, openStart: number, openEnd: number): boolean { if (source) { if (!(source instanceof LineView)) return false if (!this.dom) source.transferDOM(this) // Reuse source.dom when appropriate } if (hasStart) this.setDeco(source ? source.attrs : null) mergeChildrenInto(this, from, to, source ? source.children.slice() : [], openStart, openEnd) return true } split(at: number) { let end = new LineView end.breakAfter = this.breakAfter if (this.length == 0) return end let {i, off} = this.childPos(at) if (off) { end.append(this.children[i].split(off), 0) this.children[i].merge(off, this.children[i].length, null, false, 0, 0) i++ } for (let j = i; j < this.children.length; j++) end.append(this.children[j], 0) while (i > 0 && this.children[i - 1].length == 0) this.children[--i].destroy() this.children.length = i this.markDirty() this.length = at return end } transferDOM(other: LineView) { if (!this.dom) return this.markDirty() other.setDOM(this.dom) other.prevAttrs = this.prevAttrs === undefined ? this.attrs : this.prevAttrs this.prevAttrs = undefined this.dom = null } setDeco(attrs: Attrs | null) { if (!attrsEq(this.attrs, attrs)) { if (this.dom) { this.prevAttrs = this.attrs this.markDirty() } this.attrs = attrs } } append(child: ContentView, openStart: number) { joinInlineInto(this, child, openStart) } // Only called when building a line view in ContentBuilder addLineDeco(deco: LineDecoration) { let attrs = deco.spec.attributes, cls = deco.spec.class if (attrs) this.attrs = combineAttrs(attrs, this.attrs || {}) if (cls) this.attrs = combineAttrs({class: cls}, this.attrs || {}); } domAtPos(pos: number): DOMPos { return inlineDOMAtPos(this, pos) } reuseDOM(node: Node) { if (node.nodeName == "DIV") { this.setDOM(node) this.flags |= ViewFlag.AttrsDirty | ViewFlag.NodeDirty } } sync(view: EditorView, track?: {node: Node, written: boolean}) { if (!this.dom) { this.setDOM(document.createElement("div")) this.dom!.className = "cm-line" this.prevAttrs = this.attrs ? null : undefined } else if (this.flags & ViewFlag.AttrsDirty) { clearAttributes(this.dom) this.dom!.className = "cm-line" this.prevAttrs = this.attrs ? null : undefined } if (this.prevAttrs !== undefined) { updateAttrs(this.dom!, this.prevAttrs, this.attrs) this.dom!.classList.add("cm-line") this.prevAttrs = undefined } super.sync(view, track) let last = this.dom!.lastChild while (last && ContentView.get(last) instanceof MarkView) last = last.lastChild if (!last || !this.length || last.nodeName != "BR" && ContentView.get(last)?.isEditable == false && (!browser.ios || !this.children.some(ch => ch instanceof TextView))) { let hack = document.createElement("BR") ;(hack as any).cmIgnore = true this.dom!.appendChild(hack) } } measureTextSize(): {lineHeight: number, charWidth: number, textHeight: number} | null { if (this.children.length == 0 || this.length > 20) return null let totalWidth = 0, textHeight!: number for (let child of this.children) { if (!(child instanceof TextView) || /[^ -~]/.test(child.text)) return null let rects = clientRectsFor(child.dom!) if (rects.length != 1) return null totalWidth += rects[0].width textHeight = rects[0].height } return !totalWidth ? null : { lineHeight: this.dom!.getBoundingClientRect().height, charWidth: totalWidth / this.length, textHeight } } coordsAt(pos: number, side: number): Rect | null { let rect = coordsInChildren(this, pos, side) // Correct rectangle height for empty lines when the returned // height is larger than the text height. if (!this.children.length && rect && this.parent) { let {heightOracle} = this.parent.view.viewState, height = rect.bottom - rect.top if (Math.abs(height - heightOracle.lineHeight) < 2 && heightOracle.textHeight < height) { let dist = (height - heightOracle.textHeight) / 2 return {top: rect.top + dist, bottom: rect.bottom - dist, left: rect.left, right: rect.left} } } return rect } become(other: ContentView) { return other instanceof LineView && this.children.length == 0 && other.children.length == 0 && attrsEq(this.attrs, other.attrs) && this.breakAfter == other.breakAfter } covers() { return true } static find(docView: DocView, pos: number): LineView | null { for (let i = 0, off = 0; i < docView.children.length; i++) { let block = docView.children[i], end = off + block.length if (end >= pos) { if (block instanceof LineView) return block if (end > pos) break } off = end + block.breakAfter } return null } } export class BlockWidgetView extends ContentView implements BlockView { declare dom: HTMLElement | null declare parent: DocView | null breakAfter = 0 prevWidget: WidgetType | null = null constructor(public widget: WidgetType, public length: number, public deco: PointDecoration) { super() } merge(from: number, to: number, source: ContentView | null, _takeDeco: boolean, openStart: number, openEnd: number): boolean { if (source && (!(source instanceof BlockWidgetView) || !this.widget.compare(source.widget) || from > 0 && openStart <= 0 || to < this.length && openEnd <= 0)) return false this.length = from + (source ? source.length : 0) + (this.length - to) return true } domAtPos(pos: number) { return pos == 0 ? DOMPos.before(this.dom!) : DOMPos.after(this.dom!, pos == this.length) } split(at: number) { let len = this.length - at this.length = at let end = new BlockWidgetView(this.widget, len, this.deco) end.breakAfter = this.breakAfter return end } get children() { return noChildren } sync(view: EditorView) { if (!this.dom || !this.widget.updateDOM(this.dom, view)) { if (this.dom && this.prevWidget) this.prevWidget.destroy(this.dom) this.prevWidget = null this.setDOM(this.widget.toDOM(view)) if (!this.widget.editable) this.dom!.contentEditable = "false" } } get overrideDOMText() { return this.parent ? this.parent!.view.state.doc.slice(this.posAtStart, this.posAtEnd) : Text.empty } domBoundsAround() { return null } become(other: ContentView) { if (other instanceof BlockWidgetView && other.widget.constructor == this.widget.constructor) { if (!other.widget.compare(this.widget)) this.markDirty(true) if (this.dom && !this.prevWidget) this.prevWidget = this.widget this.widget = other.widget this.length = other.length this.deco = other.deco this.breakAfter = other.breakAfter return true } return false } ignoreMutation(): boolean { return true } ignoreEvent(event: Event): boolean { return this.widget.ignoreEvent(event) } get isEditable() { return false } get isWidget() { return true } coordsAt(pos: number, side: number) { let custom = this.widget.coordsAt(this.dom!, pos, side) if (custom) return custom if (this.widget instanceof BlockGapWidget) return null return flattenRect(this.dom!.getBoundingClientRect(), this.length ? pos == 0 : side <= 0) } destroy() { super.destroy() if (this.dom) this.widget.destroy(this.dom) } covers(side: -1 | 1) { let {startSide, endSide} = this.deco return startSide == endSide ? false : side < 0 ? startSide < 0 : endSide > 0 } } export class BlockGapWidget extends WidgetType { constructor(readonly height: number) { super() } toDOM() { let elt = document.createElement("div") elt.className = "cm-gap" this.updateDOM(elt) return elt } eq(other: BlockGapWidget) { return other.height == this.height } updateDOM(elt: HTMLElement) { elt.style.height = this.height + "px" return true } get editable() { return true } get estimatedHeight() { return this.height } ignoreEvent() { return false } } view-6.38.1/src/browser.ts000066400000000000000000000025461503537325200154210ustar00rootroot00000000000000let nav: any = typeof navigator != "undefined" ? navigator : {userAgent: "", vendor: "", platform: ""} let doc: any = typeof document != "undefined" ? document : {documentElement: {style: {}}} const ie_edge = /Edge\/(\d+)/.exec(nav.userAgent) const ie_upto10 = /MSIE \d/.test(nav.userAgent) const ie_11up = /Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(nav.userAgent) const ie = !!(ie_upto10 || ie_11up || ie_edge) const gecko = !ie && /gecko\/(\d+)/i.test(nav.userAgent) const chrome = !ie && /Chrome\/(\d+)/.exec(nav.userAgent) const webkit = "webkitFontSmoothing" in doc.documentElement.style const safari = !ie && /Apple Computer/.test(nav.vendor) const ios = safari && (/Mobile\/\w+/.test(nav.userAgent) || nav.maxTouchPoints > 2) export default { mac: ios || /Mac/.test(nav.platform), windows: /Win/.test(nav.platform), linux: /Linux|X11/.test(nav.platform), ie, ie_version: ie_upto10 ? doc.documentMode || 6 : ie_11up ? +ie_11up[1] : ie_edge ? +ie_edge[1] : 0, gecko, gecko_version: gecko ? +(/Firefox\/(\d+)/.exec(nav.userAgent) || [0, 0])[1] : 0, chrome: !!chrome, chrome_version: chrome ? +chrome[1] : 0, ios, android: /Android\b/.test(nav.userAgent), webkit, safari, webkit_version: webkit ? +(/\bAppleWebKit\/(\d+)/.exec(nav.userAgent) || [0, 0])[1] : 0, tabSize: doc.documentElement.style.tabSize != null ? "tab-size" : "-moz-tab-size" } view-6.38.1/src/buildview.ts000066400000000000000000000151571503537325200157320ustar00rootroot00000000000000import {SpanIterator, RangeSet, Text, TextIterator} from "@codemirror/state" import {DecorationSet, Decoration, PointDecoration, LineDecoration, MarkDecoration, WidgetType} from "./decoration" import {ContentView} from "./contentview" import {BlockView, LineView, BlockWidgetView} from "./blockview" import {WidgetView, TextView, MarkView, WidgetBufferView} from "./inlineview" const enum T { Chunk = 512 } const enum Buf { No = 0, Yes = 1, IfCursor = 2 } export class ContentBuilder implements SpanIterator { content: BlockView[] = [] curLine: LineView | null = null breakAtStart = 0 pendingBuffer = Buf.No bufferMarks: readonly MarkDecoration[] = [] // Set to false directly after a widget that covers the position after it atCursorPos = true openStart = -1 openEnd = -1 cursor: TextIterator text: string = "" skip: number textOff: number = 0 constructor(private doc: Text, public pos: number, public end: number, readonly disallowBlockEffectsFor: boolean[]) { this.cursor = doc.iter() this.skip = pos } posCovered() { if (this.content.length == 0) return !this.breakAtStart && this.doc.lineAt(this.pos).from != this.pos let last = this.content[this.content.length - 1] return !(last.breakAfter || last instanceof BlockWidgetView && last.deco.endSide < 0) } getLine() { if (!this.curLine) { this.content.push(this.curLine = new LineView) this.atCursorPos = true } return this.curLine } flushBuffer(active = this.bufferMarks) { if (this.pendingBuffer) { this.curLine!.append(wrapMarks(new WidgetBufferView(-1), active), active.length) this.pendingBuffer = Buf.No } } addBlockWidget(view: BlockWidgetView) { this.flushBuffer() this.curLine = null this.content.push(view) } finish(openEnd: number) { if (this.pendingBuffer && openEnd <= this.bufferMarks.length) this.flushBuffer() else this.pendingBuffer = Buf.No if (!this.posCovered() && !(openEnd && this.content.length && this.content[this.content.length - 1] instanceof BlockWidgetView)) this.getLine() } buildText(length: number, active: readonly MarkDecoration[], openStart: number) { while (length > 0) { if (this.textOff == this.text.length) { let {value, lineBreak, done} = this.cursor.next(this.skip) this.skip = 0 if (done) throw new Error("Ran out of text content when drawing inline views") if (lineBreak) { if (!this.posCovered()) this.getLine() if (this.content.length) this.content[this.content.length - 1].breakAfter = 1 else this.breakAtStart = 1 this.flushBuffer() this.curLine = null this.atCursorPos = true length-- continue } else { this.text = value this.textOff = 0 } } let take = Math.min(this.text.length - this.textOff, length, T.Chunk) this.flushBuffer(active.slice(active.length - openStart)) this.getLine().append(wrapMarks(new TextView(this.text.slice(this.textOff, this.textOff + take)), active), openStart) this.atCursorPos = true this.textOff += take length -= take openStart = 0 } } span(from: number, to: number, active: MarkDecoration[], openStart: number) { this.buildText(to - from, active, openStart) this.pos = to if (this.openStart < 0) this.openStart = openStart } point(from: number, to: number, deco: Decoration, active: MarkDecoration[], openStart: number, index: number) { if (this.disallowBlockEffectsFor[index] && deco instanceof PointDecoration) { if (deco.block) throw new RangeError("Block decorations may not be specified via plugins") if (to > this.doc.lineAt(this.pos).to) throw new RangeError("Decorations that replace line breaks may not be specified via plugins") } let len = to - from if (deco instanceof PointDecoration) { if (deco.block) { if (deco.startSide > 0 && !this.posCovered()) this.getLine() this.addBlockWidget(new BlockWidgetView(deco.widget || NullWidget.block, len, deco)) } else { let view = WidgetView.create(deco.widget || NullWidget.inline, len, len ? 0 : deco.startSide) let cursorBefore = this.atCursorPos && !view.isEditable && openStart <= active.length && (from < to || deco.startSide > 0) let cursorAfter = !view.isEditable && (from < to || openStart > active.length || deco.startSide <= 0) let line = this.getLine() if (this.pendingBuffer == Buf.IfCursor && !cursorBefore && !view.isEditable) this.pendingBuffer = Buf.No this.flushBuffer(active) if (cursorBefore) { line.append(wrapMarks(new WidgetBufferView(1), active), openStart) openStart = active.length + Math.max(0, openStart - active.length) } line.append(wrapMarks(view, active), openStart) this.atCursorPos = cursorAfter this.pendingBuffer = !cursorAfter ? Buf.No : from < to || openStart > active.length ? Buf.Yes : Buf.IfCursor if (this.pendingBuffer) this.bufferMarks = active.slice() } } else if (this.doc.lineAt(this.pos).from == this.pos) { // Line decoration this.getLine().addLineDeco(deco as LineDecoration) } if (len) { // Advance the iterator past the replaced content if (this.textOff + len <= this.text.length) { this.textOff += len } else { this.skip += len - (this.text.length - this.textOff) this.text = "" this.textOff = 0 } this.pos = to } if (this.openStart < 0) this.openStart = openStart } static build(text: Text, from: number, to: number, decorations: readonly DecorationSet[], dynamicDecorationMap: boolean[]): {content: BlockView[], breakAtStart: number, openStart: number, openEnd: number} { let builder = new ContentBuilder(text, from, to, dynamicDecorationMap) builder.openEnd = RangeSet.spans(decorations, from, to, builder) if (builder.openStart < 0) builder.openStart = builder.openEnd builder.finish(builder.openEnd) return builder } } function wrapMarks(view: ContentView, active: readonly MarkDecoration[]) { for (let mark of active) view = new MarkView(mark, [view], view.length) return view } export class NullWidget extends WidgetType { constructor(readonly tag: string) { super() } eq(other: NullWidget) { return other.tag == this.tag } toDOM() { return document.createElement(this.tag) } updateDOM(elt: HTMLElement) { return elt.nodeName.toLowerCase() == this.tag } get isHidden() { return true } static inline = new NullWidget("span") static block = new NullWidget("div") } view-6.38.1/src/contentview.ts000066400000000000000000000305251503537325200163010ustar00rootroot00000000000000import {Text} from "@codemirror/state" import {Rect, maxOffset, domIndex} from "./dom" import {EditorView} from "./editorview" // Track mutated / outdated status of a view node's DOM export const enum ViewFlag { // At least one child is dirty ChildDirty = 1, // The node itself isn't in sync with its child list NodeDirty = 2, // The node's DOM attributes might have changed AttrsDirty = 4, // Mask for all of the dirty flags Dirty = 7, // Set temporarily during a doc view update on the nodes around the // composition Composition = 8, } export class DOMPos { constructor(readonly node: Node, readonly offset: number, readonly precise = true) {} static before(dom: Node, precise?: boolean) { return new DOMPos(dom.parentNode!, domIndex(dom), precise) } static after(dom: Node, precise?: boolean) { return new DOMPos(dom.parentNode!, domIndex(dom) + 1, precise) } } export const noChildren: ContentView[] = [] export abstract class ContentView { parent: ContentView | null = null dom: Node | null = null flags: number = ViewFlag.NodeDirty abstract length: number abstract children: ContentView[] declare breakAfter: number get overrideDOMText(): Text | null { return null } get posAtStart(): number { return this.parent ? this.parent.posBefore(this) : 0 } get posAtEnd(): number { return this.posAtStart + this.length } posBefore(view: ContentView): number { let pos = this.posAtStart for (let child of this.children) { if (child == view) return pos pos += child.length + child.breakAfter } throw new RangeError("Invalid child in posBefore") } posAfter(view: ContentView): number { return this.posBefore(view) + view.length } // Will return a rectangle directly before (when side < 0), after // (side > 0) or directly on (when the browser supports it) the // given position. abstract coordsAt(_pos: number, _side: number): Rect | null sync(view: EditorView, track?: {node: Node, written: boolean}) { if (this.flags & ViewFlag.NodeDirty) { let parent = this.dom as HTMLElement let prev: Node | null = null, next for (let child of this.children) { if (child.flags & ViewFlag.Dirty) { if (!child.dom && (next = prev ? prev.nextSibling : parent.firstChild)) { let contentView = ContentView.get(next) if (!contentView || !contentView.parent && contentView.canReuseDOM(child)) child.reuseDOM(next) } child.sync(view, track) child.flags &= ~ViewFlag.Dirty } next = prev ? prev.nextSibling : parent.firstChild if (track && !track.written && track.node == parent && next != child.dom) track.written = true if (child.dom!.parentNode == parent) { while (next && next != child.dom) next = rm(next) } else { parent.insertBefore(child.dom!, next) } prev = child.dom! } next = prev ? prev.nextSibling : parent.firstChild if (next && track && track.node == parent) track.written = true while (next) next = rm(next) } else if (this.flags & ViewFlag.ChildDirty) { for (let child of this.children) if (child.flags & ViewFlag.Dirty) { child.sync(view, track) child.flags &= ~ViewFlag.Dirty } } } reuseDOM(_dom: Node) {} abstract domAtPos(pos: number): DOMPos localPosFromDOM(node: Node, offset: number): number { let after: Node | null if (node == this.dom) { after = this.dom.childNodes[offset] } else { let bias = maxOffset(node) == 0 ? 0 : offset == 0 ? -1 : 1 for (;;) { let parent = node.parentNode! if (parent == this.dom) break if (bias == 0 && parent.firstChild != parent.lastChild) { if (node == parent.firstChild) bias = -1 else bias = 1 } node = parent } if (bias < 0) after = node else after = node.nextSibling } if (after == this.dom!.firstChild) return 0 while (after && !ContentView.get(after)) after = after.nextSibling if (!after) return this.length for (let i = 0, pos = 0;; i++) { let child = this.children[i] if (child.dom == after) return pos pos += child.length + child.breakAfter } } domBoundsAround(from: number, to: number, offset = 0): { startDOM: Node | null, endDOM: Node | null, from: number, to: number } | null { let fromI = -1, fromStart = -1, toI = -1, toEnd = -1 for (let i = 0, pos = offset, prevEnd = offset; i < this.children.length; i++) { let child = this.children[i], end = pos + child.length if (pos < from && end > to) return child.domBoundsAround(from, to, pos) if (end >= from && fromI == -1) { fromI = i fromStart = pos } if (pos > to && child.dom!.parentNode == this.dom) { toI = i toEnd = prevEnd break } prevEnd = end pos = end + child.breakAfter } return {from: fromStart, to: toEnd < 0 ? offset + this.length : toEnd, startDOM: (fromI ? this.children[fromI - 1].dom!.nextSibling : null) || this.dom!.firstChild, endDOM: toI < this.children.length && toI >= 0 ? this.children[toI].dom : null} } markDirty(andParent: boolean = false) { this.flags |= ViewFlag.NodeDirty this.markParentsDirty(andParent) } markParentsDirty(childList: boolean) { for (let parent = this.parent; parent; parent = parent.parent) { if (childList) parent.flags |= ViewFlag.NodeDirty if (parent.flags & ViewFlag.ChildDirty) return parent.flags |= ViewFlag.ChildDirty childList = false } } setParent(parent: ContentView) { if (this.parent != parent) { this.parent = parent if (this.flags & ViewFlag.Dirty) this.markParentsDirty(true) } } setDOM(dom: Node) { if (this.dom == dom) return if (this.dom) (this.dom as any).cmView = null this.dom = dom ;(dom as any).cmView = this } get rootView(): ContentView { for (let v: ContentView = this;;) { let parent = v.parent if (!parent) return v v = parent } } replaceChildren(from: number, to: number, children: ContentView[] = noChildren) { this.markDirty() for (let i = from; i < to; i++) { let child = this.children[i] if (child.parent == this && children.indexOf(child) < 0) child.destroy() } if (children.length < 250) this.children.splice(from, to - from, ...children) else this.children = ([] as ContentView[]).concat(this.children.slice(0, from), children, this.children.slice(to)) for (let i = 0; i < children.length; i++) children[i].setParent(this) } ignoreMutation(_rec: MutationRecord): boolean { return false } ignoreEvent(_event: Event): boolean { return false } childCursor(pos: number = this.length) { return new ChildCursor(this.children, pos, this.children.length) } childPos(pos: number, bias: number = 1): {i: number, off: number} { return this.childCursor().findPos(pos, bias) } toString() { let name = this.constructor.name.replace("View", "") return name + (this.children.length ? "(" + this.children.join() + ")" : this.length ? "[" + (name == "Text" ? (this as any).text : this.length) + "]" : "") + (this.breakAfter ? "#" : "") } static get(node: Node): ContentView | null { return (node as any).cmView } get isEditable() { return true } get isWidget() { return false } get isHidden() { return false } merge(from: number, to: number, source: ContentView | null, hasStart: boolean, openStart: number, openEnd: number): boolean { return false } become(other: ContentView): boolean { return false } canReuseDOM(other: ContentView) { return other.constructor == this.constructor && !((this.flags | other.flags) & ViewFlag.Composition) } abstract split(at: number): ContentView // When this is a zero-length view with a side, this should return a // number <= 0 to indicate it is before its position, or a // number > 0 when after its position. getSide() { return 0 } destroy() { for (let child of this.children) if (child.parent == this) child.destroy() this.parent = null } } ContentView.prototype.breakAfter = 0 // Remove a DOM node and return its next sibling. function rm(dom: Node): Node | null { let next = dom.nextSibling dom.parentNode!.removeChild(dom) return next } export class ChildCursor { off: number = 0 constructor(public children: readonly ContentView[], public pos: number, public i: number) {} findPos(pos: number, bias: number = 1): this { for (;;) { if (pos > this.pos || pos == this.pos && (bias > 0 || this.i == 0 || this.children[this.i - 1].breakAfter)) { this.off = pos - this.pos return this } let next = this.children[--this.i] this.pos -= next.length + next.breakAfter } } } export function replaceRange(parent: ContentView, fromI: number, fromOff: number, toI: number, toOff: number, insert: ContentView[], breakAtStart: number, openStart: number, openEnd: number) { let {children} = parent let before = children.length ? children[fromI] : null let last = insert.length ? insert[insert.length - 1] : null let breakAtEnd = last ? last.breakAfter : breakAtStart // Change within a single child if (fromI == toI && before && !breakAtStart && !breakAtEnd && insert.length < 2 && before.merge(fromOff, toOff, insert.length ? last : null, fromOff == 0, openStart, openEnd)) return if (toI < children.length) { let after = children[toI] // Make sure the end of the child after the update is preserved in `after` if (after && (toOff < after.length || after.breakAfter && last?.breakAfter)) { // If we're splitting a child, separate part of it to avoid that // being mangled when updating the child before the update. if (fromI == toI) { after = after.split(toOff) toOff = 0 } // If the element after the replacement should be merged with // the last replacing element, update `content` if (!breakAtEnd && last && after.merge(0, toOff, last, true, 0, openEnd)) { insert[insert.length - 1] = after } else { // Remove the start of the after element, if necessary, and // add it to `content`. if (toOff || after.children.length && !after.children[0].length) after.merge(0, toOff, null, false, 0, openEnd) insert.push(after) } } else if (after?.breakAfter) { // The element at `toI` is entirely covered by this range. // Preserve its line break, if any. if (last) last.breakAfter = 1 else breakAtStart = 1 } // Since we've handled the next element from the current elements // now, make sure `toI` points after that. toI++ } if (before) { before.breakAfter = breakAtStart if (fromOff > 0) { if (!breakAtStart && insert.length && before.merge(fromOff, before.length, insert[0], false, openStart, 0)) { before.breakAfter = insert.shift()!.breakAfter } else if (fromOff < before.length || before.children.length && before.children[before.children.length - 1].length == 0) { before.merge(fromOff, before.length, null, false, openStart, 0) } fromI++ } } // Try to merge widgets on the boundaries of the replacement while (fromI < toI && insert.length) { if (children[toI - 1].become(insert[insert.length - 1])) { toI-- insert.pop() openEnd = insert.length ? 0 : openStart } else if (children[fromI].become(insert[0])) { fromI++ insert.shift() openStart = insert.length ? 0 : openEnd } else { break } } if (!insert.length && fromI && toI < children.length && !children[fromI - 1].breakAfter && children[toI].merge(0, 0, children[fromI - 1], false, openStart, openEnd)) fromI-- if (fromI < toI || insert.length) parent.replaceChildren(fromI, toI, insert) } export function mergeChildrenInto(parent: ContentView, from: number, to: number, insert: ContentView[], openStart: number, openEnd: number) { let cur = parent.childCursor() let {i: toI, off: toOff} = cur.findPos(to, 1) let {i: fromI, off: fromOff} = cur.findPos(from, -1) let dLen = from - to for (let view of insert) dLen += view.length parent.length += dLen replaceRange(parent, fromI, fromOff, toI, toOff, insert, 0, openStart, openEnd) } view-6.38.1/src/cursor.ts000066400000000000000000000402041503537325200152440ustar00rootroot00000000000000import {EditorState, EditorSelection, SelectionRange, RangeSet, CharCategory, findColumn, findClusterBreak} from "@codemirror/state" import {EditorView} from "./editorview" import {BlockType} from "./decoration" import {LineView} from "./blockview" import {atomicRanges} from "./extension" import {clientRectsFor, textRange, Rect, maxOffset} from "./dom" import {moveVisually, movedOver, Direction} from "./bidi" import {BlockInfo} from "./heightmap" import browser from "./browser" declare global { interface Selection { modify(action: string, direction: string, granularity: string): void } interface Document { caretPositionFromPoint(x: number, y: number): {offsetNode: Node, offset: number} } } export function groupAt(state: EditorState, pos: number, bias: 1 | -1 = 1) { let categorize = state.charCategorizer(pos) let line = state.doc.lineAt(pos), linePos = pos - line.from if (line.length == 0) return EditorSelection.cursor(pos) if (linePos == 0) bias = 1 else if (linePos == line.length) bias = -1 let from = linePos, to = linePos if (bias < 0) from = findClusterBreak(line.text, linePos, false) else to = findClusterBreak(line.text, linePos) let cat = categorize(line.text.slice(from, to)) while (from > 0) { let prev = findClusterBreak(line.text, from, false) if (categorize(line.text.slice(prev, from)) != cat) break from = prev } while (to < line.length) { let next = findClusterBreak(line.text, to) if (categorize(line.text.slice(to, next)) != cat) break to = next } return EditorSelection.range(from + line.from, to + line.from) } // Search the DOM for the {node, offset} position closest to the given // coordinates. Very inefficient and crude, but can usually be avoided // by calling caret(Position|Range)FromPoint instead. function getdx(x: number, rect: ClientRect): number { return rect.left > x ? rect.left - x : Math.max(0, x - rect.right) } function getdy(y: number, rect: ClientRect): number { return rect.top > y ? rect.top - y : Math.max(0, y - rect.bottom) } function yOverlap(a: ClientRect, b: ClientRect): boolean { return a.top < b.bottom - 1 && a.bottom > b.top + 1 } function upTop(rect: ClientRect, top: number): ClientRect { return top < rect.top ? {top, left: rect.left, right: rect.right, bottom: rect.bottom} as ClientRect : rect } function upBot(rect: ClientRect, bottom: number): ClientRect { return bottom > rect.bottom ? {top: rect.top, left: rect.left, right: rect.right, bottom} as ClientRect : rect } function domPosAtCoords(parent: HTMLElement, x: number, y: number): {node: Node, offset: number} { let closest, closestRect!: ClientRect, closestX!: number, closestY!: number, closestOverlap = false let above, below, aboveRect, belowRect for (let child: Node | null = parent.firstChild; child; child = child.nextSibling) { let rects = clientRectsFor(child) for (let i = 0; i < rects.length; i++) { let rect: ClientRect = rects[i] if (closestRect && yOverlap(closestRect, rect)) rect = upTop(upBot(rect, closestRect.bottom), closestRect.top) let dx = getdx(x, rect), dy = getdy(y, rect) if (dx == 0 && dy == 0) return child.nodeType == 3 ? domPosInText(child as Text, x, y) : domPosAtCoords(child as HTMLElement, x, y) if (!closest || closestY > dy || closestY == dy && closestX > dx) { closest = child; closestRect = rect; closestX = dx; closestY = dy closestOverlap = !dx ? true : x < rect.left ? i > 0 : i < rects.length - 1 } if (dx == 0) { if (y > rect.bottom && (!aboveRect || aboveRect.bottom < rect.bottom)) { above = child; aboveRect = rect } else if (y < rect.top && (!belowRect || belowRect.top > rect.top)) { below = child; belowRect = rect } } else if (aboveRect && yOverlap(aboveRect, rect)) { aboveRect = upBot(aboveRect, rect.bottom) } else if (belowRect && yOverlap(belowRect, rect)) { belowRect = upTop(belowRect, rect.top) } } } if (aboveRect && aboveRect.bottom >= y) { closest = above; closestRect = aboveRect } else if (belowRect && belowRect.top <= y) { closest = below; closestRect = belowRect } if (!closest) return {node: parent, offset: 0} let clipX = Math.max(closestRect!.left, Math.min(closestRect!.right, x)) if (closest.nodeType == 3) return domPosInText(closest as Text, clipX, y) if (closestOverlap && (closest as HTMLElement).contentEditable != "false") return domPosAtCoords(closest as HTMLElement, clipX, y) let offset = Array.prototype.indexOf.call(parent.childNodes, closest) + (x >= (closestRect!.left + closestRect!.right) / 2 ? 1 : 0) return {node: parent, offset} } function domPosInText(node: Text, x: number, y: number): {node: Node, offset: number} { let len = node.nodeValue!.length let closestOffset = -1, closestDY = 1e9, generalSide = 0 for (let i = 0; i < len; i++) { let rects = textRange(node, i, i + 1).getClientRects() for (let j = 0; j < rects.length; j++) { let rect = rects[j] if (rect.top == rect.bottom) continue if (!generalSide) generalSide = x - rect.left let dy = (rect.top > y ? rect.top - y : y - rect.bottom) - 1 if (rect.left - 1 <= x && rect.right + 1 >= x && dy < closestDY) { let right = x >= (rect.left + rect.right) / 2, after = right if (browser.chrome || browser.gecko) { // Check for RTL on browsers that support getting client // rects for empty ranges. let rectBefore = textRange(node, i).getBoundingClientRect() if (rectBefore.left == rect.right) after = !right } if (dy <= 0) return {node, offset: i + (after ? 1 : 0)} closestOffset = i + (after ? 1 : 0) closestDY = dy } } } return {node, offset: closestOffset > -1 ? closestOffset : generalSide > 0 ? node.nodeValue!.length : 0} } export function posAtCoords(view: EditorView, coords: {x: number, y: number}, precise: boolean, bias: -1 | 1 = -1): number | null { let content = view.contentDOM.getBoundingClientRect(), docTop = content.top + view.viewState.paddingTop let block, {docHeight} = view.viewState let {x, y} = coords, yOffset = y - docTop if (yOffset < 0) return 0 if (yOffset > docHeight) return view.state.doc.length // Scan for a text block near the queried y position for (let halfLine = view.viewState.heightOracle.textHeight / 2, bounced = false;;) { block = view.elementAtHeight(yOffset) if (block.type == BlockType.Text) break for (;;) { // Move the y position out of this block yOffset = bias > 0 ? block.bottom + halfLine : block.top - halfLine if (yOffset >= 0 && yOffset <= docHeight) break // If the document consists entirely of replaced widgets, we // won't find a text block, so return 0 if (bounced) return precise ? null : 0 bounced = true bias = -bias as -1 | 1 } } y = docTop + yOffset let lineStart = block.from // If this is outside of the rendered viewport, we can't determine a position if (lineStart < view.viewport.from) return view.viewport.from == 0 ? 0 : precise ? null : posAtCoordsImprecise(view, content, block, x, y) if (lineStart > view.viewport.to) return view.viewport.to == view.state.doc.length ? view.state.doc.length : precise ? null : posAtCoordsImprecise(view, content, block, x, y) // Prefer ShadowRootOrDocument.elementFromPoint if present, fall back to document if not let doc = view.dom.ownerDocument let root = (view.root as any).elementFromPoint ? view.root as Document : doc let element = root.elementFromPoint(x, y) if (element && !view.contentDOM.contains(element)) element = null // If the element is unexpected, clip x at the sides of the content area and try again if (!element) { x = Math.max(content.left + 1, Math.min(content.right - 1, x)) element = root.elementFromPoint(x, y) if (element && !view.contentDOM.contains(element)) element = null } // There's visible editor content under the point, so we can try // using caret(Position|Range)FromPoint as a shortcut let node: Node | undefined, offset: number = -1 if (element && view.docView.nearest(element)?.isEditable != false) { if (doc.caretPositionFromPoint) { let pos = doc.caretPositionFromPoint(x, y) if (pos) ({offsetNode: node, offset} = pos) } else if (doc.caretRangeFromPoint) { let range = doc.caretRangeFromPoint(x, y) if (range) { ;({startContainer: node, startOffset: offset} = range) if (!view.contentDOM.contains(node) || browser.safari && isSuspiciousSafariCaretResult(node, offset, x) || browser.chrome && isSuspiciousChromeCaretResult(node, offset, x)) node = undefined } } // Chrome will return offsets into elements without child // nodes, which will lead to a null deref below, so clip the // offset to the node size. if (node) offset = Math.min(maxOffset(node), offset) } // No luck, do our own (potentially expensive) search if (!node || !view.docView.dom.contains(node)) { let line = LineView.find(view.docView, lineStart) if (!line) return yOffset > block.top + block.height / 2 ? block.to : block.from ;({node, offset} = domPosAtCoords(line.dom!, x, y)) } let nearest = view.docView.nearest(node) if (!nearest) return null if (nearest.isWidget && nearest.dom?.nodeType == 1) { let rect = (nearest.dom as HTMLElement).getBoundingClientRect() return coords.y < rect.top || coords.y <= rect.bottom && coords.x <= (rect.left + rect.right) / 2 ? nearest.posAtStart : nearest.posAtEnd } else { return nearest.localPosFromDOM(node, offset) + nearest.posAtStart } } function posAtCoordsImprecise(view: EditorView, contentRect: Rect, block: BlockInfo, x: number, y: number) { let into = Math.round((x - contentRect.left) * view.defaultCharacterWidth) if (view.lineWrapping && block.height > view.defaultLineHeight * 1.5) { let textHeight = view.viewState.heightOracle.textHeight let line = Math.floor((y - block.top - (view.defaultLineHeight - textHeight) * 0.5) / textHeight) into += line * view.viewState.heightOracle.lineLength } let content = view.state.sliceDoc(block.from, block.to) return block.from + findColumn(content, into, view.state.tabSize) } // In case of a high line height, Safari's caretRangeFromPoint treats // the space between lines as belonging to the last character of the // line before. This is used to detect such a result so that it can be // ignored (issue #401). function isSuspiciousSafariCaretResult(node: Node, offset: number, x: number) { let len, scan = node if (node.nodeType != 3 || offset != (len = node.nodeValue!.length)) return false for (;;) { // Check that there is no content after this node let next = scan.nextSibling if (next) { if (next.nodeName == "BR") break return false } else { let parent = scan.parentNode if (!parent || parent.nodeName == "DIV") break scan = parent } } return textRange(node as Text, len - 1, len).getBoundingClientRect().right > x } // Chrome will move positions between lines to the start of the next line function isSuspiciousChromeCaretResult(node: Node, offset: number, x: number) { if (offset != 0) return false for (let cur = node;;) { let parent = cur.parentNode if (!parent || parent.nodeType != 1 || parent.firstChild != cur) return false if ((parent as HTMLElement).classList.contains("cm-line")) break cur = parent } let rect = node.nodeType == 1 ? (node as HTMLElement).getBoundingClientRect() : textRange(node as Text, 0, Math.max(node.nodeValue!.length, 1)).getBoundingClientRect() return x - rect.left > 5 } export function blockAt(view: EditorView, pos: number, side: -1 | 1): BlockInfo { let line = view.lineBlockAt(pos) if (Array.isArray(line.type)) { let best: BlockInfo | undefined for (let l of line.type) { if (l.from > pos) break if (l.to < pos) continue if (l.from < pos && l.to > pos) return l if (!best || (l.type == BlockType.Text && (best.type != l.type || (side < 0 ? l.from < pos : l.to > pos)))) best = l } return best || line } return line } export function moveToLineBoundary(view: EditorView, start: SelectionRange, forward: boolean, includeWrap: boolean) { let line = blockAt(view, start.head, start.assoc || -1) let coords = !includeWrap || line.type != BlockType.Text || !(view.lineWrapping || line.widgetLineBreaks) ? null : view.coordsAtPos(start.assoc < 0 && start.head > line.from ? start.head - 1 : start.head) if (coords) { let editorRect = view.dom.getBoundingClientRect() let direction = view.textDirectionAt(line.from) let pos = view.posAtCoords({x: forward == (direction == Direction.LTR) ? editorRect.right - 1 : editorRect.left + 1, y: (coords.top + coords.bottom) / 2}) if (pos != null) return EditorSelection.cursor(pos, forward ? -1 : 1) } return EditorSelection.cursor(forward ? line.to : line.from, forward ? -1 : 1) } export function moveByChar(view: EditorView, start: SelectionRange, forward: boolean, by?: (initial: string) => (next: string) => boolean) { let line = view.state.doc.lineAt(start.head), spans = view.bidiSpans(line) let direction = view.textDirectionAt(line.from) for (let cur = start, check: null | ((next: string) => boolean) = null;;) { let next = moveVisually(line, spans, direction, cur, forward), char = movedOver if (!next) { if (line.number == (forward ? view.state.doc.lines : 1)) return cur char = "\n" line = view.state.doc.line(line.number + (forward ? 1 : -1)) spans = view.bidiSpans(line) next = view.visualLineSide(line, !forward) } if (!check) { if (!by) return next check = by(char) } else if (!check(char)) { return cur } cur = next } } export function byGroup(view: EditorView, pos: number, start: string) { let categorize = view.state.charCategorizer(pos) let cat = categorize(start) return (next: string) => { let nextCat = categorize(next) if (cat == CharCategory.Space) cat = nextCat return cat == nextCat } } export function moveVertically(view: EditorView, start: SelectionRange, forward: boolean, distance?: number) { let startPos = start.head, dir: -1 | 1 = forward ? 1 : -1 if (startPos == (forward ? view.state.doc.length : 0)) return EditorSelection.cursor(startPos, start.assoc) let goal = start.goalColumn, startY let rect = view.contentDOM.getBoundingClientRect() let startCoords = view.coordsAtPos(startPos, start.assoc || -1), docTop = view.documentTop if (startCoords) { if (goal == null) goal = startCoords.left - rect.left startY = dir < 0 ? startCoords.top : startCoords.bottom } else { let line = view.viewState.lineBlockAt(startPos) if (goal == null) goal = Math.min(rect.right - rect.left, view.defaultCharacterWidth * (startPos - line.from)) startY = (dir < 0 ? line.top : line.bottom) + docTop } let resolvedGoal = rect.left + goal let dist = distance ?? (view.viewState.heightOracle.textHeight >> 1) for (let extra = 0;; extra += 10) { let curY = startY + (dist + extra) * dir let pos = posAtCoords(view, {x: resolvedGoal, y: curY}, false, dir)! if (curY < rect.top || curY > rect.bottom || (dir < 0 ? pos < startPos : pos > startPos)) { let charRect = view.docView.coordsForChar(pos) let assoc = !charRect || curY < charRect.top ? -1 : 1 return EditorSelection.cursor(pos, assoc, undefined, goal) } } } export function skipAtomicRanges(atoms: readonly RangeSet[], pos: number, bias: -1 | 0 | 1) { for (;;) { let moved = 0 for (let set of atoms) { set.between(pos - 1, pos + 1, (from, to, value) => { if (pos > from && pos < to) { let side = moved || bias || (pos - from < to - pos ? -1 : 1) pos = side < 0 ? from : to moved = side } }) } if (!moved) return pos } } export function skipAtoms(view: EditorView, oldPos: SelectionRange, pos: SelectionRange) { let newPos = skipAtomicRanges(view.state.facet(atomicRanges).map(f => f(view)), pos.from, oldPos.head > pos.from ? -1 : 1) return newPos == pos.from ? pos : EditorSelection.cursor(newPos, newPos < pos.from ? 1 : -1) } view-6.38.1/src/decoration.ts000066400000000000000000000354731503537325200160720ustar00rootroot00000000000000import {MapMode, RangeValue, Range, RangeSet} from "@codemirror/state" import {Direction} from "./bidi" import {attrsEq, Attrs} from "./attributes" import {EditorView} from "./editorview" import {Rect} from "./dom" interface MarkDecorationSpec { /// Whether the mark covers its start and end position or not. This /// influences whether content inserted at those positions becomes /// part of the mark. Defaults to false. inclusive?: boolean /// Specify whether the start position of the marked range should be /// inclusive. Overrides `inclusive`, when both are present. inclusiveStart?: boolean /// Whether the end should be inclusive. inclusiveEnd?: boolean /// Add attributes to the DOM elements that hold the text in the /// marked range. attributes?: {[key: string]: string} /// Shorthand for `{attributes: {class: value}}`. class?: string /// Add a wrapping element around the text in the marked range. Note /// that there will not necessarily be a single element covering the /// entire range—other decorations with lower precedence might split /// this one if they partially overlap it, and line breaks always /// end decoration elements. tagName?: string /// When using sets of decorations in /// [`bidiIsolatedRanges`](##view.EditorView^bidiIsolatedRanges), /// this property provides the direction of the isolates. When null /// or not given, it indicates the range has `dir=auto`, and its /// direction should be derived from the first strong directional /// character in it. bidiIsolate?: Direction | null /// Decoration specs allow extra properties, which can be retrieved /// through the decoration's [`spec`](#view.Decoration.spec) /// property. [other: string]: any } interface WidgetDecorationSpec { /// The type of widget to draw here. widget: WidgetType /// Which side of the given position the widget is on. When this is /// positive, the widget will be drawn after the cursor if the /// cursor is on the same position. Otherwise, it'll be drawn before /// it. When multiple widgets sit at the same position, their `side` /// values will determine their ordering—those with a lower value /// come first. Defaults to 0. May not be more than 10000 or less /// than -10000. side?: number /// By default, to avoid unintended mixing of block and inline /// widgets, block widgets with a positive `side` are always drawn /// after all inline widgets at that position, and those with a /// non-positive side before inline widgets. Setting this option to /// `true` for a block widget will turn this off and cause it to be /// rendered between the inline widgets, ordered by `side`. inlineOrder?: boolean /// Determines whether this is a block widgets, which will be drawn /// between lines, or an inline widget (the default) which is drawn /// between the surrounding text. /// /// Note that block-level decorations should not have vertical /// margins, and if you dynamically change their height, you should /// make sure to call /// [`requestMeasure`](#view.EditorView.requestMeasure), so that the /// editor can update its information about its vertical layout. block?: boolean /// Other properties are allowed. [other: string]: any } interface ReplaceDecorationSpec { /// An optional widget to drawn in the place of the replaced /// content. widget?: WidgetType /// Whether this range covers the positions on its sides. This /// influences whether new content becomes part of the range and /// whether the cursor can be drawn on its sides. Defaults to false /// for inline replacements, and true for block replacements. inclusive?: boolean /// Set inclusivity at the start. inclusiveStart?: boolean /// Set inclusivity at the end. inclusiveEnd?: boolean /// Whether this is a block-level decoration. Defaults to false. block?: boolean /// Other properties are allowed. [other: string]: any } interface LineDecorationSpec { /// DOM attributes to add to the element wrapping the line. attributes?: {[key: string]: string} /// Shorthand for `{attributes: {class: value}}`. class?: string /// Other properties are allowed. [other: string]: any } /// Widgets added to the content are described by subclasses of this /// class. Using a description object like that makes it possible to /// delay creating of the DOM structure for a widget until it is /// needed, and to avoid redrawing widgets even if the decorations /// that define them are recreated. export abstract class WidgetType { /// Build the DOM structure for this widget instance. abstract toDOM(view: EditorView): HTMLElement /// Compare this instance to another instance of the same type. /// (TypeScript can't express this, but only instances of the same /// specific class will be passed to this method.) This is used to /// avoid redrawing widgets when they are replaced by a new /// decoration of the same type. The default implementation just /// returns `false`, which will cause new instances of the widget to /// always be redrawn. eq(widget: WidgetType): boolean { return false } /// Update a DOM element created by a widget of the same type (but /// different, non-`eq` content) to reflect this widget. May return /// true to indicate that it could update, false to indicate it /// couldn't (in which case the widget will be redrawn). The default /// implementation just returns false. updateDOM(dom: HTMLElement, view: EditorView): boolean { return false } /// @internal compare(other: WidgetType): boolean { return this == other || this.constructor == other.constructor && this.eq(other) } /// The estimated height this widget will have, to be used when /// estimating the height of content that hasn't been drawn. May /// return -1 to indicate you don't know. The default implementation /// returns -1. get estimatedHeight(): number { return -1 } /// For inline widgets that are displayed inline (as opposed to /// `inline-block`) and introduce line breaks (through `
` tags /// or textual newlines), this must indicate the amount of line /// breaks they introduce. Defaults to 0. get lineBreaks(): number { return 0 } /// Can be used to configure which kinds of events inside the widget /// should be ignored by the editor. The default is to ignore all /// events. ignoreEvent(event: Event): boolean { return true } /// Override the way screen coordinates for positions at/in the /// widget are found. `pos` will be the offset into the widget, and /// `side` the side of the position that is being queried—less than /// zero for before, greater than zero for after, and zero for /// directly at that position. coordsAt(dom: HTMLElement, pos: number, side: number): Rect | null { return null } /// @internal get isHidden() { return false } /// @internal get editable() { return false } /// This is called when the an instance of the widget is removed /// from the editor view. destroy(dom: HTMLElement) {} } /// A decoration set represents a collection of decorated ranges, /// organized for efficient access and mapping. See /// [`RangeSet`](#state.RangeSet) for its methods. export type DecorationSet = RangeSet const enum Side { NonIncEnd = -6e8, // (end of non-inclusive range) GapStart = -5e8, BlockBefore = -4e8, // + widget side option (block widget before) BlockIncStart = -3e8, // (start of inclusive block range) Line = -2e8, // (line widget) InlineBefore = -1e8, // + widget side (inline widget before) InlineIncStart = -1, // (start of inclusive inline range) InlineIncEnd = 1, // (end of inclusive inline range) InlineAfter = 1e8, // + widget side (inline widget after) BlockIncEnd = 2e8, // (end of inclusive block range) BlockAfter = 3e8, // + widget side (block widget after) GapEnd = 4e8, NonIncStart = 5e8 // (start of non-inclusive range) } /// The different types of blocks that can occur in an editor view. export enum BlockType { /// A line of text. Text, /// A block widget associated with the position after it. WidgetBefore, /// A block widget associated with the position before it. WidgetAfter, /// A block widget [replacing](#view.Decoration^replace) a range of content. WidgetRange } /// A decoration provides information on how to draw or style a piece /// of content. You'll usually use it wrapped in a /// [`Range`](#state.Range), which adds a start and end position. /// @nonabstract export abstract class Decoration extends RangeValue { protected constructor( /// @internal readonly startSide: number, /// @internal readonly endSide: number, /// @internal readonly widget: WidgetType | null, /// The config object used to create this decoration. You can /// include additional properties in there to store metadata about /// your decoration. readonly spec: any) { super() } /// @internal declare point: boolean /// @internal get heightRelevant() { return false } abstract eq(other: Decoration): boolean /// Create a mark decoration, which influences the styling of the /// content in its range. Nested mark decorations will cause nested /// DOM elements to be created. Nesting order is determined by /// precedence of the [facet](#view.EditorView^decorations), with /// the higher-precedence decorations creating the inner DOM nodes. /// Such elements are split on line boundaries and on the boundaries /// of lower-precedence decorations. static mark(spec: MarkDecorationSpec): Decoration { return new MarkDecoration(spec) } /// Create a widget decoration, which displays a DOM element at the /// given position. static widget(spec: WidgetDecorationSpec): Decoration { let side = Math.max(-10000, Math.min(10000, spec.side || 0)), block = !!spec.block side += (block && !spec.inlineOrder) ? (side > 0 ? Side.BlockAfter : Side.BlockBefore) : (side > 0 ? Side.InlineAfter : Side.InlineBefore) return new PointDecoration(spec, side, side, block, spec.widget || null, false) } /// Create a replace decoration which replaces the given range with /// a widget, or simply hides it. static replace(spec: ReplaceDecorationSpec): Decoration { let block = !!spec.block, startSide, endSide if (spec.isBlockGap) { startSide = Side.GapStart endSide = Side.GapEnd } else { let {start, end} = getInclusive(spec, block) startSide = (start ? (block ? Side.BlockIncStart : Side.InlineIncStart) : Side.NonIncStart) - 1 endSide = (end ? (block ? Side.BlockIncEnd : Side.InlineIncEnd) : Side.NonIncEnd) + 1 } return new PointDecoration(spec, startSide, endSide, block, spec.widget || null, true) } /// Create a line decoration, which can add DOM attributes to the /// line starting at the given position. static line(spec: LineDecorationSpec): Decoration { return new LineDecoration(spec) } /// Build a [`DecorationSet`](#view.DecorationSet) from the given /// decorated range or ranges. If the ranges aren't already sorted, /// pass `true` for `sort` to make the library sort them for you. static set(of: Range | readonly Range[], sort = false): DecorationSet { return RangeSet.of(of, sort) } /// The empty set of decorations. static none = RangeSet.empty as DecorationSet /// @internal hasHeight() { return this.widget ? this.widget.estimatedHeight > -1 : false } } export class MarkDecoration extends Decoration { tagName: string class: string attrs: Attrs | null constructor(spec: MarkDecorationSpec) { let {start, end} = getInclusive(spec) super(start ? Side.InlineIncStart : Side.NonIncStart, end ? Side.InlineIncEnd : Side.NonIncEnd, null, spec) this.tagName = spec.tagName || "span" this.class = spec.class || "" this.attrs = spec.attributes || null } eq(other: Decoration): boolean { return this == other || other instanceof MarkDecoration && this.tagName == other.tagName && (this.class || this.attrs?.class) == (other.class || other.attrs?.class) && attrsEq(this.attrs, other.attrs, "class") } range(from: number, to = from) { if (from >= to) throw new RangeError("Mark decorations may not be empty") return super.range(from, to) } } MarkDecoration.prototype.point = false export class LineDecoration extends Decoration { constructor(spec: LineDecorationSpec) { super(Side.Line, Side.Line, null, spec) } eq(other: Decoration): boolean { return other instanceof LineDecoration && this.spec.class == other.spec.class && attrsEq(this.spec.attributes, other.spec.attributes) } range(from: number, to = from) { if (to != from) throw new RangeError("Line decoration ranges must be zero-length") return super.range(from, to) } } LineDecoration.prototype.mapMode = MapMode.TrackBefore LineDecoration.prototype.point = true export class PointDecoration extends Decoration { constructor(spec: any, startSide: number, endSide: number, public block: boolean, widget: WidgetType | null, readonly isReplace: boolean) { super(startSide, endSide, widget, spec) this.mapMode = !block ? MapMode.TrackDel : startSide <= 0 ? MapMode.TrackBefore : MapMode.TrackAfter } // Only relevant when this.block == true get type() { return this.startSide != this.endSide ? BlockType.WidgetRange : this.startSide <= 0 ? BlockType.WidgetBefore : BlockType.WidgetAfter } get heightRelevant() { return this.block || !!this.widget && (this.widget.estimatedHeight >= 5 || this.widget.lineBreaks > 0) } eq(other: Decoration): boolean { return other instanceof PointDecoration && widgetsEq(this.widget, other.widget) && this.block == other.block && this.startSide == other.startSide && this.endSide == other.endSide } range(from: number, to = from) { if (this.isReplace && (from > to || (from == to && this.startSide > 0 && this.endSide <= 0))) throw new RangeError("Invalid range for replacement decoration") if (!this.isReplace && to != from) throw new RangeError("Widget decorations can only have zero-length ranges") return super.range(from, to) } } PointDecoration.prototype.point = true function getInclusive(spec: { inclusive?: boolean, inclusiveStart?: boolean, inclusiveEnd?: boolean }, block = false): {start: boolean, end: boolean} { let {inclusiveStart: start, inclusiveEnd: end} = spec if (start == null) start = spec.inclusive if (end == null) end = spec.inclusive return {start: start ?? block, end: end ?? block} } function widgetsEq(a: WidgetType | null, b: WidgetType | null): boolean { return a == b || !!(a && b && a.compare(b)) } export function addRange(from: number, to: number, ranges: number[], margin = 0) { let last = ranges.length - 1 if (last >= 0 && ranges[last] + margin >= from) ranges[last] = Math.max(ranges[last], to) else ranges.push(from, to) } view-6.38.1/src/dialog.ts000066400000000000000000000135421503537325200151730ustar00rootroot00000000000000import {StateField, StateEffect} from "@codemirror/state" import {showPanel, Panel, PanelConstructor, getPanel} from "./panel" import elt from "crelt" import {EditorView} from "./editorview" type DialogConfig = { /// A function to render the content of the dialog. The result /// should contain at least one `
` element. Submit handlers a /// handler for the Escape key will be added to the form. /// /// If this is not given, the `label`, `input`, and `submitLabel` /// fields will be used to create a simple form for you. content?: (view: EditorView, close: () => void) => HTMLElement /// When `content` isn't given, this provides the text shown in the /// dialog. label?: string /// The attributes for an input element shown next to the label. If /// not given, no input element is added. input?: {[attr: string]: string} /// The label for the button that submits the form. Defaults to /// `"OK"`. submitLabel?: string, /// Extra classes to add to the panel. class?: string /// A query selector to find the field that should be focused when /// the dialog is opened. When set to true, this picks the first /// `` or `