Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax highlighting and collapsing with fountain format for script editor #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion src/components/editors/ScriptEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function ScriptEditor() {
const publish = useScriptEditor((s) => s.publish)
const onDidScrollChange = useScriptEditor((s) => s.onDidScrollChange)
const jumpCursorOnLineClick = useScriptEditor((s) => s.jumpCursorOnLineClick)
const highlightElements = useScriptEditor((s) => s.highlightElements)

const scrollHeight = useScriptEditor((s) => s.scrollHeight)

Expand Down Expand Up @@ -105,6 +106,7 @@ export function ScriptEditor() {
}
)
*/
highlightElements()
}

const setMonaco = useScriptEditor((s) => s.setMonaco)
Expand All @@ -125,6 +127,14 @@ export function ScriptEditor() {
inherit: true, // Inherit the default rules
rules: [
// You can define token-specific styles here if needed
{ token: 'scene.int', foreground: '#4EC9B0' },
{ token: 'scene.ext', foreground: '#9CDCFE' },
{ token: 'character', foreground: '#DCDCAA' },
{ token: 'dialog', foreground: '#D4D4D4' },
{ token: 'parenthetical', foreground: '#B5CEA8' },
{ token: 'transition', foreground: '#C586C0' },
{ token: 'shot', foreground: '#CE9178' },
{ token: 'action', foreground: '#D4D4D4' },
],
colors: {
'editor.background':
Expand All @@ -149,9 +159,60 @@ export function ScriptEditor() {

const textModel: MonacoEditor.editor.ITextModel = monaco.editor.createModel(
current || '',
'plaintext'
'fountain'
)
setTextModel(textModel)

// Register fountain language
monaco.languages.register({ id: 'fountain' })
monaco.languages.setMonarchTokensProvider('fountain', {
tokenizer: {
root: [
[/^(INT|I\/E)(.+)/, 'scene.int'],
[/^(EXT)(.+)/, 'scene.ext'],
[/^[A-Z][A-Z\s]+$/, 'character'],
[/^\(.+\)$/, 'parenthetical'],
[/^>.+<$/, 'transition'],
[/^(?:FADE (?:IN|OUT|TO)|CUT TO:)$/, 'transition'],
[/^(?:ANGLE ON|CLOSE ON|PAN|TRACKING|MOVING):.+$/, 'shot'],
[/^(?![A-Z]+$|\(|\>).+/, 'action'],
],
},
})

// Setup code folding
monaco.languages.registerFoldingRangeProvider('fountain', {
provideFoldingRanges: function (model, context, token) {
const lines = model.getLinesContent()
const ranges = []
let sceneStart = -1

for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim()
if (/^(INT|EXT|I\/E)/.test(line)) {
if (sceneStart !== -1) {
ranges.push({
start: sceneStart + 1,
end: i,
kind: monaco.languages.FoldingRangeKind.Region,
})
}
sceneStart = i
}
}

// Add the last scene if there is one
if (sceneStart !== -1 && sceneStart < lines.length - 1) {
ranges.push({
start: sceneStart + 1,
end: lines.length,
kind: monaco.languages.FoldingRangeKind.Region,
})
}

return ranges
},
})
}

return (
Expand All @@ -169,6 +230,9 @@ export function ScriptEditor() {
onChange={setCurrent}
options={{
fontSize: editorFontSize,
language: 'fountain',
folding: true,
foldingStrategy: 'auto',
}}
/>
</div>
Expand Down
Loading