66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
import type { PageFormatId } from "@/lib/drive/page-formats"
|
|
import {
|
|
formatRulerMajorLabel,
|
|
getRulerUnitConfig,
|
|
} from "@/lib/drive/docs-ruler-units"
|
|
|
|
export type DocsRulerTick = {
|
|
/** Position in page px. */
|
|
pos: number
|
|
major: boolean
|
|
label?: string
|
|
}
|
|
|
|
function buildRulerTicksAlongAxis(
|
|
lengthPx: number,
|
|
formatId: PageFormatId,
|
|
originOffsetPx = 0
|
|
): DocsRulerTick[] {
|
|
const { unit, majorStepPx, minorDivisions } = getRulerUnitConfig(formatId)
|
|
const minorStep = majorStepPx / minorDivisions
|
|
const ticks: DocsRulerTick[] = []
|
|
|
|
const startMajor = -Math.ceil(originOffsetPx / majorStepPx)
|
|
const endMajor = Math.ceil(lengthPx / majorStepPx)
|
|
|
|
for (let major = startMajor; major <= endMajor; major += 1) {
|
|
const pos = major * majorStepPx
|
|
if (pos > lengthPx + 0.5) break
|
|
|
|
const unitValue = major
|
|
ticks.push({
|
|
pos,
|
|
major: true,
|
|
label:
|
|
unitValue !== 0 || originOffsetPx > 0
|
|
? formatRulerMajorLabel(unitValue, unit)
|
|
: undefined,
|
|
})
|
|
|
|
if (pos < 0) continue
|
|
|
|
for (let minor = 1; minor < minorDivisions; minor += 1) {
|
|
const tickPos = pos + minor * minorStep
|
|
if (tickPos > lengthPx) break
|
|
ticks.push({ pos: tickPos, major: false })
|
|
}
|
|
}
|
|
|
|
return ticks
|
|
}
|
|
|
|
export function buildHorizontalRulerTicks(
|
|
lengthPx: number,
|
|
formatId: PageFormatId
|
|
): DocsRulerTick[] {
|
|
return buildRulerTicksAlongAxis(lengthPx, formatId, 0)
|
|
}
|
|
|
|
export function buildVerticalRulerTicks(
|
|
pageHeightPx: number,
|
|
topMarginPx: number,
|
|
formatId: PageFormatId
|
|
): DocsRulerTick[] {
|
|
return buildRulerTicksAlongAxis(pageHeightPx, formatId, topMarginPx)
|
|
}
|