I’m working on trying to improve caret positioning for our inline-code (mark) text. The current situation has the caret positioned offset from where typed characters appear:
Ideally the caret renders in the position where text will actually appear. The above example using the CSS:
.code {
padding: 2px 4px;
background: #fefefe;
border: 1px solid #ccc;
font-family: monospace;
}
The situation can be improved by avoiding horizontal padding, and opting for spacer pseudo elements instead:
.code::before,
.code::after {
display: inline-block;
width: 5px;
background: transparent;
height: 1px;
content: '';
}
.code {
padding: 2px 0;
background: #fefefe;
border: 1px solid #ccc;
font-family: monospace;
}
This yields the improved behaviour:
All is good until you:
- Make a selection containing a few leading characters.
- Delete those characters.
- Type a letter.
After step (2), the browser places the caret outside the element:
However ProseMirror overrides that and places the caret back inside the element, and upon typing a character inserts that character outside the element.
Apart from the ProseMirror behaviour, the experience is pretty decent. It’s a bit awkward in Chrome because caret positions are collapsed at node boundaries, but in Firefox (which doesn’t seem to collapse caret positions) it’s quite pleasant:
So a couple of questions:
- Is it viable to improve ProseMirror’s reading from DOM behaviour to better interpret text insertion around the boundaries of an element (in this case some marked text) so that the native caret position and insertion location is honored?
- Even if (1) was successful, the behaviour would remain awkward in Chrome. Ideally there’d be two caret positions straddling the boundary of the element (as Firefox does) so that the user can decide whether to insert text inside or outside a mark. Do we have an idiomatic / recommended approach to this on the horizon? (e.g. perhaps something using node views and zero-width characters)