Files
edentext/src/lib/editor/extensions/bulletList.ts
T
Steffen Becker 63149d52d7 feat: a list marker sits at the level's hanging indent
CSS gives ::marker no position, so Chromium set it flush against the
text — about 4mm right of where Word and LibreOffice draw it. The marker
is now the item block's ::before, placed at the hanging indent both
exports already write (LIST_HANGING_CM 0.635cm): measured against LO's
own render, the three bullet levels land within 0.04mm.

Out of flow, because in flow it grew every list line by a pixel and cost
a fixture fixture a page. `list-style: none` drops only the marker box,
so the list-item counter and <ol start> keep numbering; a bullet's own
char rides an inherited property, since attr() in a pseudo would read the
block rather than the list.
2026-08-08 12:57:24 +00:00

67 lines
2.4 KiB
TypeScript

import BulletListBase from '@tiptap/extension-bullet-list';
import { bulletCharAttr } from '../../utils/bulletListTypes';
declare module '@tiptap/core' {
interface Commands<ReturnType> {
customBulletList: {
/** Set the marker char of the innermost bullet list at the cursor (null = default cycle). */
setBulletChar: (char: string | null) => ReturnType;
};
}
}
// BulletList with a `bulletChar` attr (a literal marker char, null = the default
// per-depth cycle), rendered as `data-bullet` + a CSS string marker on the <ul>:
// export/odt.ts maps it to text:bullet-char, export/docx.ts to w:lvlText.
export const BulletList = BulletListBase.extend({
addAttributes() {
return {
...this.parent?.(),
bulletChar: {
default: null,
parseHTML: (element) => element.getAttribute('data-bullet') || null,
renderHTML: (attributes) => {
const ch = attributes.bulletChar;
// The marker is the item block's ::before, whose attr() would read that block,
// not the list — so the char travels as an inherited custom property instead.
return ch ? { 'data-bullet': ch, style: `--bullet: "${ch}"` } : {};
},
},
};
},
addCommands() {
return {
...this.parent?.(),
// setNodeMarkup on exactly the innermost list: updateAttributes would also
// rewrite every ancestor bulletList (it visits all nodes across the range).
setBulletChar:
(char) =>
({ state, tr, dispatch }) => {
const { $from } = state.selection;
for (let d = $from.depth; d > 0; d--) {
const node = $from.node(d);
if (node.type.name !== this.name) continue;
// Suppress the char when it matches the default cycle at this depth.
const value = bulletCharAttr(char, countListDepth($from, d));
if (dispatch) {
tr.setNodeMarkup($from.before(d), undefined, { ...node.attrs, bulletChar: value });
}
return true;
}
return false;
},
};
},
});
// 0-based bullet/ordered-list nesting depth of the list node at resolved depth `d`.
function countListDepth($from: { node: (d: number) => { type: { name: string } } }, d: number): number {
let depth0 = 0;
for (let i = d - 1; i > 0; i--) {
const name = $from.node(i).type.name;
if (name === 'bulletList' || name === 'orderedList') depth0++;
}
return depth0;
}