Skip to content

Commit 2087d99

Browse files
committed
timetravel -- refactoring code to make it possible to implement git support
1 parent 6bb52d6 commit 2087d99

File tree

12 files changed

+239
-187
lines changed

12 files changed

+239
-187
lines changed

src/packages/frontend/editors/task-editor/history-viewer.tsx

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,18 @@ History viewer for Tasks notebooks --- very similar to same file in jupyter/ di
1010
import { Checkbox } from "antd";
1111
import { fromJS, Map } from "immutable";
1212
import TaskList from "./list";
13-
import { CSS, React, useState } from "../../app-framework";
13+
import { useState } from "../../app-framework";
1414
import { cmp } from "@cocalc/util/misc";
15-
import { SyncDB } from "@cocalc/sync/editor/db";
1615
import { Tasks } from "./types";
1716

18-
const SHOW_DONE_STYLE: CSS = {
17+
const SHOW_DONE_STYLE = {
1918
fontSize: "12pt",
2019
color: "#666",
2120
padding: "5px 15px",
2221
borderBottom: "1px solid lightgrey",
2322
} as const;
2423

25-
interface Props {
26-
syncdb: SyncDB;
27-
version: Date;
28-
font_size: number;
29-
}
30-
31-
export const TasksHistoryViewer: React.FC<Props> = ({
32-
syncdb,
33-
version,
34-
font_size,
35-
}) => {
24+
export function TasksHistoryViewer({ doc, project_id, path, font_size }) {
3625
const [show_done, set_show_done] = useState(false);
3726

3827
function render_task_list(doc) {
@@ -50,8 +39,8 @@ export const TasksHistoryViewer: React.FC<Props> = ({
5039

5140
return (
5241
<TaskList
53-
path={syncdb.get_path()}
54-
project_id={syncdb.get_project_id()}
42+
path={path}
43+
project_id={project_id}
5544
tasks={tasks}
5645
visible={visible}
5746
read_only={true}
@@ -60,7 +49,6 @@ export const TasksHistoryViewer: React.FC<Props> = ({
6049
);
6150
}
6251

63-
const doc = syncdb.version(version);
6452
return (
6553
<div
6654
style={{
@@ -81,4 +69,4 @@ export const TasksHistoryViewer: React.FC<Props> = ({
8169
{doc == null ? <span>Unknown version</span> : render_task_list(doc)}
8270
</div>
8371
);
84-
};
72+
}

src/packages/frontend/file-associations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import videoExtensions from "video-extensions";
2323
import audioExtensions from "audio-extensions";
2424
import { filename_extension } from "@cocalc/util/misc";
2525

26-
export function filenameMode(path: string): string {
27-
return file_associations[filename_extension(path)]?.opts?.mode ?? "text";
26+
export function filenameMode(path: string, fallback = "text"): string {
27+
return file_associations[filename_extension(path)]?.opts?.mode ?? fallback;
2828
}
2929

3030
const codemirror_associations: { [ext: string]: string } = {

src/packages/frontend/frame-editors/time-travel-editor/actions.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
} from "../code-editor/actions";
3636
import { FrameTree } from "../frame-tree/types";
3737
import { export_to_json } from "./export-to-json";
38+
import type { Document } from "@cocalc/sync/editor/generic/types";
3839

3940
const EXTENSION = ".time-travel";
4041

@@ -51,6 +52,7 @@ export interface TimeTravelState extends CodeEditorState {
5152
has_full_history: boolean;
5253
docpath: string;
5354
docext: string;
55+
git?: boolean;
5456
//frame_states: Map<string, any>; // todo: really map from frame_id to FrameState as immutable map.
5557
}
5658

@@ -83,6 +85,7 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
8385
docext: this.docext,
8486
});
8587
this.init_syncdoc();
88+
this.updateGitVersions();
8689
}
8790

8891
public _raw_default_frame_tree(): FrameTree {
@@ -151,10 +154,6 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
151154
}
152155

153156
private syncdoc_changed(): void {
154-
if (this.store.get("git_mode")) {
155-
this.updateGitVersions();
156-
return;
157-
}
158157
if (this.syncdoc == null) return;
159158
if (this.syncdoc?.get_state() != "ready") {
160159
return;
@@ -191,10 +190,7 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
191190
}
192191

193192
// Get the given version of the document.
194-
public get_doc(version: Date): any {
195-
if (this.store.get("git_mode")) {
196-
return this.gitShow(version);
197-
}
193+
public get_doc(version: Date): Document | undefined {
198194
if (this.syncdoc == null) return;
199195
const state = this.syncdoc.get_state();
200196
if (state != "ready") return;
@@ -306,7 +302,7 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
306302
if (node == null) continue;
307303
text_mode = !!text_mode;
308304
actions.set_frame_tree({ id, text_mode });
309-
return;
305+
break;
310306
}
311307
}
312308

@@ -317,10 +313,11 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
317313
if (node == null) continue;
318314
git_mode = !!git_mode;
319315
actions.set_frame_tree({ id, git_mode });
320-
return;
316+
break;
321317
}
322318
this.updateGitVersions();
323319
}
320+
324321
public set_versions(id: string, version0: number, version1: number): void {
325322
for (const actions of [this, this.ambient_actions]) {
326323
if (actions == null || actions._get_frame_node(id) == null) continue;
@@ -402,14 +399,19 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
402399
this.gitTimestampToHash[t] = h.trim();
403400
versions.push(new Date(t));
404401
}
405-
this.setState({ versions: List<Date>(versions) });
402+
versions.reverse();
403+
this.setState({
404+
git: versions.length > 0,
405+
versions: List<Date>(versions),
406+
});
406407
} catch (err) {
407408
this.set_error(`${err}`);
409+
this.setState({ git: false });
408410
return;
409411
}
410412
};
411413

412-
private gitShow = async (version: Date): Promise<string> => {
414+
gitShow = async (version: Date): Promise<string> => {
413415
const h = this.gitTimestampToHash[version.valueOf()];
414416
if (h == null) {
415417
return "";

src/packages/frontend/frame-editors/time-travel-editor/document.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Render a static version of a document for use in TimeTravel.
1010
import { fromJS } from "immutable";
1111
import { CodemirrorEditor } from "../code-editor/codemirror-editor";
1212

13-
export function Document(props) {
13+
export function TextDocument(props) {
1414
return (
1515
<div className="smc-vfill" style={{ overflowY: "auto" }}>
1616
<CodemirrorEditor

src/packages/frontend/frame-editors/time-travel-editor/navigation-slider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ export function NavigationSlider({
3535

3636
return (
3737
<Slider
38+
style={{ margin: "10px 15px" }}
3839
min={0}
3940
max={max}
4041
value={version}
4142
onChange={(value) => {
4243
actions.set_version(id, value);
4344
}}
44-
tooltip={{ formatter: renderTooltip, placement: "bottom", open: true }}
45+
tooltip={{ formatter: renderTooltip, placement: "bottom" }}
4546
/>
4647
);
4748
}

src/packages/frontend/frame-editors/time-travel-editor/range-slider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function RangeSlider({
6565
paddingTop: "48px",
6666
paddingBottom: "20px",
6767
width: "90%",
68-
margin: "auto",
68+
margin: "10px 15px",
6969
}}
7070
>
7171
<Slider

src/packages/frontend/frame-editors/time-travel-editor/sagews-codemirror.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@ code to get this done for now.
1010

1111
import { debounce } from "lodash";
1212
import * as CodeMirror from "codemirror";
13-
import { Map } from "immutable";
1413
import { useEffect, useRef, MutableRefObject } from "react";
15-
const { codemirror_editor } = require("../../editor");
16-
const { SynchronizedWorksheet } = require("../../sagews/sagews");
14+
const { codemirror_editor } = require("@cocalc/frontend/editor");
15+
const { SynchronizedWorksheet } = require("@cocalc/frontend/sagews/sagews");
1716
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
1817

1918
interface Props {
20-
content: string;
19+
doc;
2120
path: string;
2221
project_id: string;
2322
font_size: number;
24-
editor_settings: Map<string, any>;
2523
}
2624

27-
export function SagewsCodemirror(props: Props) {
25+
export function SagewsCodemirror({ doc, path, project_id, font_size }: Props) {
2826
const { isVisible } = useFrameContext();
2927
const updateRef = useRef<Function>(null) as MutableRefObject<Function>;
3028
const cmRef = useRef<CodeMirror.Editor | null>(
@@ -41,7 +39,7 @@ export function SagewsCodemirror(props: Props) {
4139
}
4240

4341
const opts = { mode: "sagews", read_only: true };
44-
viewDocRef.current = codemirror_editor(props.project_id, props.path, opts);
42+
viewDocRef.current = codemirror_editor(project_id, path, opts);
4543
cmRef.current = viewDocRef.current.codemirror;
4644
// insert it into the dom.
4745
$(viewDocRef.current.element).appendTo($(div));
@@ -54,14 +52,14 @@ export function SagewsCodemirror(props: Props) {
5452
};
5553
const worksheet = new SynchronizedWorksheet(viewDocRef.current, opts0);
5654

57-
const f = (content: string): void => {
55+
const f = (value: string): void => {
5856
if (viewDocRef.current == null) {
5957
return;
6058
}
61-
cmRef.current?.setValueNoJump(content);
59+
cmRef.current?.setValueNoJump(value);
6260
worksheet.process_sage_updates();
6361
};
64-
f(props.content);
62+
f(doc.to_str());
6563
updateRef.current = debounce(f, 100);
6664
};
6765

@@ -76,10 +74,10 @@ export function SagewsCodemirror(props: Props) {
7674
}, []);
7775

7876
useEffect(() => {
79-
updateRef.current?.(props.content);
80-
viewDocRef.current?.set_font_size(cmRef.current, props.font_size);
77+
updateRef.current?.(doc.to_str());
78+
viewDocRef.current?.set_font_size(cmRef.current, font_size);
8179
cmRef.current?.refresh();
82-
}, [props.font_size, props.content, isVisible]);
80+
}, [font_size, doc, isVisible]);
8381

8482
return (
8583
<div className="smc-vfill" style={{ overflow: "auto" }}>

0 commit comments

Comments
 (0)