-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add quote support * feat: quote style
- Loading branch information
1 parent
26e21b6
commit ec86a36
Showing
6 changed files
with
156 additions
and
66 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,63 @@ | ||
import { elem, removeNode } from "blocky-common/es/dom"; | ||
import { EditorState } from "@pkg/model"; | ||
import { BlockDataElement, Changeset } from "@pkg/data"; | ||
import { Subject, fromEvent, takeUntil } from "rxjs"; | ||
|
||
export class LeftPadRenderer { | ||
readonly dispose$ = new Subject<void>(); | ||
constructor(readonly container: HTMLDivElement) {} | ||
render() {} | ||
dispose(): void { | ||
this.dispose$.next(); | ||
removeNode(this.container); | ||
} | ||
} | ||
const checkedColor = "rgb(240, 153, 56)"; | ||
export class CheckboxRenderer extends LeftPadRenderer { | ||
#checkboxContainer: HTMLDivElement; | ||
#centerElement: HTMLDivElement; | ||
#checked = false; | ||
constructor( | ||
container: HTMLDivElement, | ||
private state: EditorState, | ||
private blockElement: BlockDataElement | ||
) { | ||
super(container); | ||
this.#checkboxContainer = elem("div", "blocky-checkbox"); | ||
container.append(this.#checkboxContainer); | ||
|
||
this.#centerElement = elem("div", "blocky-checkbox-center"); | ||
this.#centerElement.style.backgroundColor = checkedColor; | ||
this.#checkboxContainer.appendChild(this.#centerElement); | ||
this.#centerElement.style.visibility = "hidden"; | ||
this.#checkboxContainer.style.boxShadow = `0px 0px 0px 1px gray`; | ||
|
||
fromEvent(this.#checkboxContainer, "click") | ||
.pipe(takeUntil(this.dispose$)) | ||
.subscribe(this.#handleClick); | ||
} | ||
|
||
#handleClick = () => { | ||
const checked = !!this.blockElement.getAttribute("checked"); | ||
new Changeset(this.state) | ||
.updateAttributes(this.blockElement, { checked: !checked }) | ||
.apply({ | ||
refreshCursor: true, | ||
}); | ||
}; | ||
|
||
override render(): void { | ||
const checked = !!this.blockElement.getAttribute("checked"); | ||
if (checked == this.#checked) { | ||
return; | ||
} | ||
if (checked) { | ||
this.#centerElement.style.visibility = ""; | ||
this.#checkboxContainer.style.boxShadow = `0px 0px 0px 1px ${checkedColor}`; | ||
} else { | ||
this.#centerElement.style.visibility = "hidden"; | ||
this.#checkboxContainer.style.boxShadow = `0px 0px 0px 1px gray`; | ||
} | ||
this.#checked = checked; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { isWhiteSpace } from "blocky-common/es"; | ||
import { type IPlugin, TextBlock, type PluginContext } from "@pkg/index"; | ||
import { Changeset, TextType } from "@pkg/data"; | ||
import Delta from "quill-delta-es"; | ||
import { isNumber, isString } from "lodash-es"; | ||
import { filter, takeUntil } from "rxjs"; | ||
|
||
function makeQuotePlugin(): IPlugin { | ||
return { | ||
name: "quote", | ||
onInitialized(context: PluginContext) { | ||
const { editor, dispose$ } = context; | ||
editor.textInput$ | ||
.pipe( | ||
takeUntil(dispose$), | ||
filter((evt) => evt.blockElement.t === TextBlock.Name) // don't apply on Title block | ||
) | ||
.subscribe((evt) => { | ||
const { beforeString, blockElement } = evt; | ||
const { state } = editor; | ||
const changeset = new Changeset(state); | ||
const delta = new Delta(); | ||
|
||
let index = 0; | ||
for (const op of evt.applyDelta.ops) { | ||
if (isString(op.insert)) { | ||
const before = beforeString.slice(0, index); | ||
if (isWhiteSpace(op.insert)) { | ||
if (before === "|") { | ||
delta.delete(2); | ||
changeset.updateAttributes(blockElement, { | ||
textType: TextType.Quote, | ||
}); | ||
} | ||
break; | ||
} | ||
index += op.insert.length; | ||
} else if (isNumber(op.retain)) { | ||
index += op.retain; | ||
} | ||
} | ||
|
||
changeset.textEdit(blockElement, "textContent", () => delta); | ||
changeset.apply(); | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
export default makeQuotePlugin; |
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
ec86a36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
blocky-editor – ./
blocky-editor.dev
blocky-editor-vincentdchan.vercel.app
blocky-editor.vercel.app
blocky-editor-git-master-vincentdchan.vercel.app
www.blocky-editor.dev