Skip to content

Commit

Permalink
feat: Add start number to ordered list
Browse files Browse the repository at this point in the history
  • Loading branch information
1943time committed Dec 28, 2023
1 parent 6e610ae commit a3b6ba8
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/renderer/src/editor/elements/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const List = observer(({element, attributes, children}: ElementProps<List
/>
</span>
}
{createElement(tag, {className: 'm-list'}, children)}
{createElement(tag, {className: 'm-list', start: element.start}, children)}
</div>
)
}, [element, element.children, configStore.config.dragToSort])
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/src/editor/output/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ export const toMarkdown = (tree: any[], preString = '', parent: any[] = [{root:
if (p.type === 'list-item') {
const list = parent[parent.length - 2]
let pre = preString + (list.order ? (space + ' ') : space)
const index = list.children.findIndex(c => c === p)
let index = list.children.findIndex(c => c === p)
if (list.start) index += (list.start - 1)
if (i === 0) {
str += preString
str += list.order ? `${index + 1}. ` : '- '
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/editor/parser/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const parserBlock = (nodes: Content[], top = false, parent?: Content) => {
el = {type: 'inline-katex', children: [{text: n.value}]} as InlineKatexNode
break
case 'list':
el = {type: 'list', order: n.ordered, children: parserBlock(n.children, false, n)}
el = {type: 'list', order: n.ordered, start: n.start, children: parserBlock(n.children, false, n)}
break
case 'footnoteReference':
if (share) {
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/src/editor/plugins/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ export const MdElements: Record<string, MdNode> = {
Transforms.delete(editor, {
at: path
})
const start = match[1].match(/^\s*(\d+)\./)
Transforms.insertNodes(editor, {
type: 'list',
order: /^\s*\d+\./.test(match[1]),
order: !!start,
start: start ? +start[1] : undefined,
children: [
{
type: 'list-item',
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/el.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type TableNode = {type: 'table', children: TableRowNode[]}
export type TableRowNode = {type: 'table-row', children: TableCellNode[]}
export type TableCellNode = {type: 'table-cell', title?: boolean, align?: Align, children: BaseElement['children']}
export type BlockQuoteNode = {type: 'blockquote', children: (BlockQuoteNode | ParagraphNode)[]}
export type ListNode = {type: 'list', children: ListItemNode[], order?: boolean}
export type ListNode = {type: 'list', children: ListItemNode[], order?: boolean, start?: number}
export type ListItemNode = {type: 'list-item', children: BaseElement['children'], checked?: boolean}
export type HeadNode = {type: 'head', children: BaseElement['children'], level: number}
export type HrNode = {type: 'hr'}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/utils/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const formatList = (editor: Editor, node: NodeEntry<any>, type: string) => {
reverse: true,
mode: 'lowest'
}))

Transforms.setNodes(editor, {start: undefined}, {at: Path.parent(parent[1])})
for (let l of listItems) {
Transforms.setNodes(editor, {checked: task ? l[0].checked || false : undefined}, {at: l[1]})
}
Expand Down

0 comments on commit a3b6ba8

Please sign in to comment.