This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Position, Range, window } from 'vscode' | ||
import type { TextDocument, TextEditor } from 'vscode' | ||
import type { SlideInfo } from '@slidev/types' | ||
import { ctx } from './ctx' | ||
|
||
const dividerDecoration = window.createTextEditorDecorationType({ | ||
color: '#8884', | ||
isWholeLine: true, | ||
}) | ||
const frontmatterDecoration = window.createTextEditorDecorationType({ | ||
isWholeLine: true, | ||
backgroundColor: '#8881', | ||
borderColor: '#8882', | ||
border: '1px', | ||
}) | ||
|
||
export function updateAnnotaions(doc: TextDocument, editor: TextEditor) { | ||
const dividerRanges: Range[] = [] | ||
const frontmetterRanges: Range[] = [] | ||
|
||
const text = doc.getText() | ||
|
||
if (ctx.data) { | ||
const max = doc.lineCount - 1 | ||
ctx.data.slides.forEach((i) => { | ||
const item = i.inline as SlideInfo || i | ||
|
||
if (item?.start == null) | ||
return | ||
|
||
const line = [ | ||
doc.lineAt(Math.max(0, Math.min(max, item.start))), | ||
doc.lineAt(Math.max(0, Math.min(max, item.start - 1))), | ||
doc.lineAt(Math.max(0, Math.min(max, item.start + 1))), | ||
].find(i => i.text.startsWith('---')) | ||
if (!line) | ||
return null | ||
const start = new Position(line.lineNumber, 0) | ||
dividerRanges.push( | ||
new Range(start, new Position(line.lineNumber, line.text.length)), | ||
) | ||
|
||
const hasFrontmatter = Object.keys(i.frontmatter).length > 0 | ||
if (!hasFrontmatter) { | ||
frontmetterRanges.push(new Range(start, start)) | ||
} | ||
else { | ||
const range = text.slice(doc.offsetAt(start)) | ||
const match = range.match(/^---[\s\S]*?\n---/) | ||
if (match && match.index != null) { | ||
const endLine = doc.positionAt(doc.offsetAt(start) + match.index + match[0].length).line | ||
frontmetterRanges.push(new Range(start, new Position(endLine, 0))) | ||
if (endLine !== start.line) | ||
dividerRanges.push(new Range(new Position(endLine, 0), new Position(endLine, 0))) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
editor?.setDecorations( | ||
dividerDecoration, | ||
dividerRanges, | ||
) | ||
editor.setDecorations( | ||
frontmatterDecoration, | ||
frontmetterRanges, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters