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

Render preface #97

Merged
merged 3 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion web-client/src/ui/doc/Doc.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#doc-container {
display: flex;
flex-direction: column;
height: 100%;
--note-min-width: 48px;
}

#doc-content-container {
display: flex;
}

#doc-scroll {
height: 100%;
/* make document scrollable */
Expand All @@ -18,7 +24,10 @@
}

#doc-end {
height: 200px;
flex: 1;
text-align: center;
padding: 32px;
color: #888888;
}

#doc-side {
Expand Down Expand Up @@ -157,3 +166,8 @@
padding: 4px 0 4px 4px;
word-break: break-word;
}

.doc-preface-block {
padding: 4px 0px 4px 8px;
box-sizing: border-box;
}
5 changes: 3 additions & 2 deletions web-client/src/ui/doc/DocController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Debouncer } from "low/utils";
import { GameCoord } from "low/compiler.g";

import {
DocContainerId,
DocLog,
DocScrollId,
findLineByIndex,
Expand Down Expand Up @@ -146,7 +147,7 @@ export class DocController {
needUpdateCurrentLine = true;
} else {
const { scrollTop, scrollBottom } = scrollView;
const containerOffsetY = getScrollContainerOffsetY();
const containerOffsetY = getScrollContainerOffsetY(DocContainerId);
const {
scrollTop: currentLineTop,
scrollBottom: currentLineBottom,
Expand Down Expand Up @@ -251,7 +252,7 @@ export class DocController {

// Scroll the current line to visible
const { scrollTop, scrollBottom } = scrollView;
const containerOffsetY = getScrollContainerOffsetY();
const containerOffsetY = getScrollContainerOffsetY(DocContainerId);
const { scrollTop: currentLineTop, scrollBottom: currentLineBottom } =
getLineScrollView(newCurrentLine, containerOffsetY);

Expand Down
7 changes: 6 additions & 1 deletion web-client/src/ui/doc/DocLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { DocDiagnostic } from "low/compiler.g";
import { useActions } from "low/store";

import { Rich } from "./Rich";
import { DocLineContainerClass } from "./utils";
import { DocLineContainerClass, findLineByIndex } from "./utils";
import { Poor } from "./Poor";
import { updateNotePositions } from "./updateNotePositions";

/// One line in the document
type DocLineProps = {
Expand Down Expand Up @@ -63,6 +64,10 @@ export const DocLine: React.FC<DocLineProps> = ({
section: sectionIndex,
line: lineIndex,
});
const line = findLineByIndex(sectionIndex, lineIndex);
if (line) {
updateNotePositions(line);
}
}}
>
{counterText && (
Expand Down
97 changes: 57 additions & 40 deletions web-client/src/ui/doc/DocRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DocLine } from "./DocLine";
import { DocSection } from "./DocSection";
import {
DocContainerId,
DocContentContainerId,
DocLog,
DocScrollId,
resolveTag,
Expand All @@ -19,6 +20,7 @@ import {
import { DocNoteBlock, DocNoteBlockProps } from "./DocNoteBlock";
import { DocNoteContainerId } from "./updateNotePositions";
import { DocController, initDocController } from "./DocController";
import { Poor } from "./Poor";

export const DocRoot: React.FC = () => {
const { isEditingLayout } = useSelector(viewSelector);
Expand Down Expand Up @@ -91,49 +93,64 @@ const DocInternal: React.FC<DocInternalProps> = ({ document, controller }) => {
}}
>
<div id={DocContainerId}>
<div id="doc-main">
{document.route.map(({ name, lines }, i) => (
<DocSection index={i} key={i} name={name}>
{lines.map((line, j) => (
<DocLine
sectionIndex={i}
lineIndex={j}
key={j}
diagnostics={line.diagnostics}
lineColor={line.lineColor}
text={resolveTags(tagMap, line.text)}
iconUrl={
line.icon
? document.project.icons[line.icon]
: undefined
}
secondaryText={resolveTags(
tagMap,
line.secondaryText,
)}
counterText={
line.counterText
? resolveTag(
tagMap,
line.counterText,
)
: undefined
}
counterType={
line.counterText?.tag || undefined
}
/>
))}
</DocSection>
<div id="doc-preface-container">
{document.preface.map((text, i) => (
<div className="doc-preface-block">
<Poor
key={i}
content={text}
textProps={{ size: 400 }}
/>
</div>
))}
<div id="doc-end">
You have reached the end of the document :))
</div>
<div id={DocContentContainerId}>
<div id="doc-main">
{document.route.map(({ name, lines }, i) => (
<DocSection index={i} key={i} name={name}>
{lines.map((line, j) => (
<DocLine
sectionIndex={i}
lineIndex={j}
key={j}
diagnostics={line.diagnostics}
lineColor={line.lineColor}
text={resolveTags(tagMap, line.text)}
iconUrl={
line.icon
? document.project.icons[
line.icon
]
: undefined
}
secondaryText={resolveTags(
tagMap,
line.secondaryText,
)}
counterText={
line.counterText
? resolveTag(
tagMap,
line.counterText,
)
: undefined
}
counterType={
line.counterText?.tag || undefined
}
/>
))}
</DocSection>
))}
</div>
<div id={DocNoteContainerId}>
{flatNotes.map((props, i) => (
<DocNoteBlock key={i} {...props} />
))}
</div>
</div>
<div id={DocNoteContainerId}>
{flatNotes.map((props, i) => (
<DocNoteBlock key={i} {...props} />
))}
<div id="doc-end">
There's nothing more to see past this point.
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion web-client/src/ui/doc/findVisibleLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const findVisibleLines = (): HTMLElement[] => {
if (!containerElement) {
return [];
}
const containerOffsetY = getScrollContainerOffsetY();
const containerOffsetY = getScrollContainerOffsetY(DocContainerId);
// get all lines
// This is always in the right order because querySelectorAll uses pre-order traversal
// Therefore we can optimize the search
Expand Down
3 changes: 2 additions & 1 deletion web-client/src/ui/doc/updateNotePositions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getLineLocationFromElement,
getScrollContainerOffsetY,
findLineByIndex,
DocContentContainerId,
} from "./utils";

/// The id of the note panel
Expand Down Expand Up @@ -38,7 +39,7 @@ export const updateNotePositions = (baseLine: HTMLElement): void => {
}
// Cancel the previous async updates
updateNotesSerial += 1;
const containerOffsetY = getScrollContainerOffsetY();
const containerOffsetY = getScrollContainerOffsetY(DocContentContainerId);

// Layout the base note
const baseNoteBlock = noteContainer.children[baseIndex] as HTMLElement;
Expand Down
11 changes: 8 additions & 3 deletions web-client/src/ui/doc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const DocLog = new Logger("doc");
export const DocScrollId = "doc-scroll";
/// The id of the container of doc view
export const DocContainerId = "doc-container";
/// The id of the container of main doc content (excluding preface)
export const DocContentContainerId = "doc-content-container";
/// Class for the doc line container
export const DocLineContainerClass = "docline-container";

Expand Down Expand Up @@ -78,11 +80,14 @@ export const getLineLocationFromElement = (
return [sectionIndex, lineIndex];
};

/// Get the offset of the scroll container
/// Get the offset of the scroll container relative to baseElementId
///
/// use DocContainerId for relative to the entire document
/// use DocContentContainerId for relative to the main content
///
/// line.getBoundingClientRect().y - containerOffsetY = line position relative to container
export const getScrollContainerOffsetY = (): number => {
const containerElement = document.getElementById(DocContainerId);
export const getScrollContainerOffsetY = (baseElementId: string): number => {
const containerElement = document.getElementById(baseElementId);
if (!containerElement) {
return 0;
}
Expand Down
23 changes: 17 additions & 6 deletions web-themes/src/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
#doc-container {
color: white;
}
@media (prefers-color-scheme: light) {
#doc-container a {
color: #8888ff;
}
}

#doc-preface-container {
background-color: #a2c4c4;
color: black;
}
#doc-preface-container a {
color: #0000ff !important;
}
.doc-preface-block {
border-bottom: 3px solid #446666;
}

/* Section banner */
.docsection-head {
Expand Down Expand Up @@ -75,7 +91,7 @@
background-color: #ff6666;
}
.docline-diagnostic-body.docline-diagnostic-error a {
color: #0000ff;
color: #0000ff !important;
}
/* Warning */
.docline-diagnostic-head.docline-diagnostic-warn {
Expand Down Expand Up @@ -104,8 +120,3 @@
background-color: #bbbbbb !important;
}
}

/* Ending block */
#doc-end {
background-color: #001a00;
}
13 changes: 10 additions & 3 deletions web-themes/src/granatus.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
#doc-container {
color: black;
}
#doc-container a {
color: #0000ff !important;
}

#doc-preface-container {
background-color: #a2c4c4;
}
.doc-preface-block {
border-top: 3px solid #446666;
}

.docsection-head {
background-color: #3388ff;
Expand Down Expand Up @@ -64,9 +74,6 @@
.docline-diagnostic-body.docline-diagnostic-error {
background-color: #ff6666;
}
.docline-diagnostic-body.docline-diagnostic-error a {
color: #0000ff;
}
/* Warning */
.docline-diagnostic-head.docline-diagnostic-warn {
background-color: #883300;
Expand Down