Skip to content

Commit

Permalink
refactor: add some types in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Dec 2, 2023
1 parent a804006 commit 945489e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
46 changes: 26 additions & 20 deletions lib/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import InlineRenderer from "@muya/inlineRenderer";
import Search from "@muya/search";
import Selection from "@muya/selection";
import JSONState from "@muya/state";
import { TState } from "@muya/state/types";
import { Nullable } from "@muya/types";
import { hasPick } from "@muya/utils";
import logger from "@muya/utils/logger";
import * as otText from "ot-text-unicode";
Expand All @@ -22,8 +24,19 @@ class Editor {
public searchModule: Search;
public clipboard: Clipboard;
public history: History;
public scrollPage: ScrollPage;
private _activeContentBlock: Content | null = null;
public scrollPage: Nullable<ScrollPage> = null;
private _activeContentBlock: Nullable<Content> = null;

constructor(public muya: Muya) {
const state = muya.options.json || muya.options.markdown || "";

this.jsonState = new JSONState(muya, state);
this.inlineRenderer = new InlineRenderer(muya);
this.selection = new Selection(muya);
this.searchModule = new Search(muya);
this.clipboard = Clipboard.create(muya);
this.history = new History(muya);
}

get activeContentBlock() {
return this._activeContentBlock;
Expand All @@ -42,26 +55,18 @@ class Editor {
}
}

constructor(public muya: Muya) {
const state = muya.options.json || muya.options.markdown || "";
this.jsonState = new JSONState(muya, state);
this.inlineRenderer = new InlineRenderer(muya);
this.selection = new Selection(muya);
this.searchModule = new Search(muya);
this.clipboard = Clipboard.create(muya);
this.history = new History(muya);
}

init() {
const { muya } = this;
const state = this.jsonState.getState();

this.scrollPage = ScrollPage.create(muya, state);
this.dispatchEvents();
this.focus();

this._dispatchEvents();
this._focus();
this.exportAPI();
}

dispatchEvents() {
private _dispatchEvents() {
const { eventCenter } = this.muya;
const { domNode } = this.muya;

Expand Down Expand Up @@ -123,9 +128,9 @@ class Editor {
eventCenter.attachDOMEvent(domNode, "compositionstart", eventHandler);
}

focus() {
private _focus() {
// TODO: the cursor maybe passed by muya options.cursor, and no need to find the first leaf block.
const firstLeafBlock = this.scrollPage.firstContentInDescendant();
const firstLeafBlock = this.scrollPage!.firstContentInDescendant();

const cursor = {
path: firstLeafBlock.path,
Expand Down Expand Up @@ -302,14 +307,15 @@ class Editor {
}
}

setContent(content, autoFocus = false) {
setContent(content: TState[] | string, autoFocus = false) {
this.jsonState.setContent(content);
const state = this.jsonState.getState();

this.scrollPage.updateState(state);
this.scrollPage!.updateState(state);
this.history.clear();

if (autoFocus) {
this.focus();
this._focus();
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/history/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Muya from "@muya/index";
import { Nullable } from "@muya/types";
import type { Doc, JSONOpList } from "ot-json1";
import * as json1 from "ot-json1";
import { TSelection } from "../selection/types";
Expand Down Expand Up @@ -31,7 +32,7 @@ const DEFAULT_OPTIONS = {
class History {
private lastRecorded: number = 0;
private ignoreChange: boolean = false;
private selectionStack: (TSelection | null)[] = [];
private selectionStack: (Nullable<TSelection>)[] = [];
private stack: Stack = {
undo: [],
redo: [],
Expand Down
6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import I18n from "@muya/i18n/index";
import Ui from "@muya/ui/ui";

import Search from "@muya/search";
import { MuyaOptions } from "./types";
import { IMuyaOptions } from "./types";

import { ISearchOption } from "./search/types";
import { TState } from "./state/types";
Expand All @@ -32,7 +32,7 @@ class Muya {
public readonly version: string =
typeof window.MUYA_VERSION === "undefined" ? "dev" : window.MUYA_VERSION;

public options: MuyaOptions;
public options: IMuyaOptions;
public eventCenter: EventCenter;
public domNode: HTMLElement;
public editor: Editor;
Expand Down Expand Up @@ -120,7 +120,7 @@ class Muya {
/**
* [ensureContainerDiv ensure container element is div]
*/
function getContainer(originContainer: HTMLElement, options: MuyaOptions) {
function getContainer(originContainer: HTMLElement, options: IMuyaOptions) {
const { spellcheckEnabled, hideQuickInsertHint } = options;
const newContainer = document.createElement("div");
const attrs = originContainer.attributes;
Expand Down
7 changes: 5 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export interface MuyaOptions {
import { TState } from "./state/types";

export interface IMuyaOptions {
fontSize: number;
lineHeight: number;
focusMode: boolean;
markdown: string;
trimUnnecessaryCodeBlockEmptyLines: boolean;
preferLooseListItem: boolean;
autoPairBracket: boolean;
Expand Down Expand Up @@ -33,6 +34,8 @@ export interface MuyaOptions {
[key: string]: string;
}
},
json?: TState[];
markdown?: string;
}

export type Nullable<T> = T | null;
Expand Down

0 comments on commit 945489e

Please sign in to comment.