mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 06:35:44 +01:00
Spell colwidth as a comma list and a block's marks by name
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { AdfAttributes } from './document.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
|
||||
export type AttributeKind = 'boolean' | 'json' | 'number' | 'string'
|
||||
export type AttributeKind = 'boolean' | 'json' | 'number' | 'numbers' | 'string'
|
||||
|
||||
export type AttributeVocabulary = Readonly<Record<string, AttributeKind>>
|
||||
|
||||
@@ -9,6 +9,7 @@ export type VocabularyValue =
|
||||
| { kind: 'boolean'; value: boolean }
|
||||
| { kind: 'json'; value: JsonValue }
|
||||
| { kind: 'number'; value: number }
|
||||
| { kind: 'numbers'; value: number[] }
|
||||
| { kind: 'string'; value: string }
|
||||
|
||||
export type VocabularyPair = VocabularyValue & { key: string }
|
||||
@@ -29,6 +30,7 @@ export function vocabularyPairs(attrs: AdfAttributes, vocabulary: AttributeVocab
|
||||
function vocabularyPair(key: string, value: JsonValue, kind: AttributeKind): VocabularyPair | undefined {
|
||||
if (kind === 'boolean') return typeof value === 'boolean' ? { key, kind, value } : undefined
|
||||
if (kind === 'number') return typeof value === 'number' ? { key, kind, value } : undefined
|
||||
if (kind === 'numbers') return Array.isArray(value) && value.length > 0 && value.every((item) => typeof item === 'number') ? { key, kind, value } : undefined
|
||||
if (kind === 'string') return typeof value === 'string' ? { key, kind, value } : undefined
|
||||
return { key, kind, value }
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ export type BlockNodeModel = {
|
||||
const cellAttributes: AttributeVocabulary = {
|
||||
background: 'string',
|
||||
colspan: 'number',
|
||||
colwidth: 'json',
|
||||
colwidth: 'numbers',
|
||||
localId: 'string',
|
||||
rowspan: 'number',
|
||||
valign: 'string',
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
import type { AttributeVocabulary } from './attribute-vocabulary.ts'
|
||||
|
||||
// Marks only a block's marks key holds, so no inline spelling names them.
|
||||
export const blockMarkAttributes = {
|
||||
alignment: { align: 'string' },
|
||||
breakout: { mode: 'string', width: 'number' },
|
||||
dataConsumer: { sources: 'json' },
|
||||
fragment: { localId: 'string', name: 'string' },
|
||||
indentation: { level: 'number' },
|
||||
} satisfies Readonly<Record<string, AttributeVocabulary>>
|
||||
|
||||
export const markAttributes = {
|
||||
border: { color: 'string', size: 'number' },
|
||||
code: {},
|
||||
@@ -12,8 +21,14 @@ export const markAttributes = {
|
||||
underline: {},
|
||||
} satisfies Readonly<Record<string, AttributeVocabulary>>
|
||||
|
||||
const blockMarkVocabularies: ReadonlyMap<string, AttributeVocabulary> = new Map(Object.entries(blockMarkAttributes))
|
||||
|
||||
export type MarkType = keyof typeof markAttributes
|
||||
|
||||
export function isMarkType(type: string): type is MarkType {
|
||||
return Object.hasOwn(markAttributes, type)
|
||||
}
|
||||
|
||||
export function markVocabulary(type: string): AttributeVocabulary | undefined {
|
||||
return isMarkType(type) ? markAttributes[type] : blockMarkVocabularies.get(type)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user