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

Feat/resize image #84

Merged
merged 7 commits into from
Nov 21, 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
7 changes: 5 additions & 2 deletions packages/blocky-example/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {
BlockyEditor,
makeReactSpanner,
makeReactToolbar,
makeImageBlockPlugin,
type SpannerRenderProps,
} from "blocky-react";
import SearchBox from "@pkg/components/searchBox";
import { makeImageBlockPlugin } from "./plugins/imageBlock";
import ImagePlaceholder from "@pkg/components/imagePlaceholder";
import { makeCommandPanelPlugin } from "./plugins/commandPanel";
import { makeAtPanelPlugin } from "./plugins/atPanel";
import SpannerMenu from "./spannerMenu";
Expand All @@ -23,7 +24,9 @@ import "blocky-core/css/blocky-core.css";

function makeEditorPlugins(): IPlugin[] {
return [
makeImageBlockPlugin(),
makeImageBlockPlugin({
placeholder: ({ setSrc }) => <ImagePlaceholder setSrc={setSrc} />,
}),
makeCommandPanelPlugin(),
makeAtPanelPlugin(),
];
Expand Down
12 changes: 9 additions & 3 deletions packages/blocky-example/app/noTitle/noTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Component, ReactNode, createRef } from "react";
import { BlockyEditor, makeReactToolbar } from "blocky-react";
import {
BlockyEditor,
makeReactToolbar,
makeImageBlockPlugin,
} from "blocky-react";
import { EditorController, IPlugin } from "blocky-core";
import { makeImageBlockPlugin } from "@pkg/app/plugins/imageBlock";
import ImagePlaceholder from "@pkg/components/imagePlaceholder";
import { makeCommandPanelPlugin } from "@pkg/app/plugins/commandPanel";
import { makeAtPanelPlugin } from "@pkg/app/plugins/atPanel";
import ToolbarMenu from "@pkg/app/toolbarMenu";
import { timer, Subject, takeUntil } from "rxjs";

function makeEditorPlugins(): IPlugin[] {
return [
makeImageBlockPlugin(),
makeImageBlockPlugin({
placeholder: ({ setSrc }) => <ImagePlaceholder setSrc={setSrc} />,
}),
makeCommandPanelPlugin(),
makeAtPanelPlugin(),
];
Expand Down
21 changes: 0 additions & 21 deletions packages/blocky-example/app/plugins/imageBlock.scss

This file was deleted.

119 changes: 0 additions & 119 deletions packages/blocky-example/app/plugins/imageBlock.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion packages/blocky-example/app/spannerMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, type RefObject, createRef } from "react";
import { type EditorController, BlockDataElement, TextType } from "blocky-core";
import Dropdown from "@pkg/components/dropdown";
import { Menu, MenuItem, Divider } from "@pkg/components/menu";
import { ImageBlockName } from "@pkg/app/plugins/imageBlock";
import { ImageBlockName } from "blocky-react";
import { Subject, takeUntil } from "rxjs";
import "./spannerMenu.scss";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.fileSelector {
position: absolute;
width: 1px;
height: 1px;
top: 0px;
left: 0px;
opacity: 0;
}
36 changes: 36 additions & 0 deletions packages/blocky-example/components/imagePlaceholder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use client";
import { useRef } from "react";
import Button from "@pkg/components/button";
import styles from "./imagePlaceholder.module.css";

function ImagePlaceholder({ setSrc }: { setSrc: (src: string) => void }) {
const selectorRef = useRef<HTMLInputElement>(null);
const handleUpload = () => {
selectorRef.current?.click();
};
const handleSelectedFileChanged = () => {
const files = selectorRef.current?.files;
if (!files || files.length === 0) {
return;
}
const fr = new FileReader();
fr.onload = () => {
setSrc(fr.result as string);
};
fr.readAsDataURL(files[0]);
};
return (
<>
<Button onClick={handleUpload}>Upload</Button>
<input
type="file"
accept=".jpg, .png, .jpeg, .gif, .bmp, .tif, .tiff|image/*"
className={styles.fileSelector}
onChange={handleSelectedFileChanged}
ref={selectorRef}
/>
</>
);
}

export default ImagePlaceholder;
4 changes: 3 additions & 1 deletion packages/blocky-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
"blocky-core": "workspace:^3.3.0"
},
"dependencies": {
"@emotion/react": "^11.11.1",
"blocky-common": "workspace:3.3.0",
"lodash-es": "*",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"rxjs": "^7.8.1"
}
}
25 changes: 10 additions & 15 deletions packages/blocky-react/src/blockActiveDetector.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { useEffect, useState, useRef } from "react";
import { useEffect, useState, useRef, useContext } from "react";
import {
type EditorController,
type CursorChangedEvent,
type CursorStateUpdateEvent,
} from "blocky-core";
import { ReactBlockContext } from "./reactBlock";

export interface BlockActiveDetectorProps {
controller: EditorController;
blockId: string;
}

export function useBlockActive(props: BlockActiveDetectorProps): boolean {
export function useBlockActive(): boolean {
const ctx = useContext(ReactBlockContext)!;
const [active, setActive] = useState<boolean>(false);
const cursorUpdateHandler = useRef<
((e: CursorStateUpdateEvent) => void) | undefined
>(undefined);

const { controller, blockId } = props;
const { editorController: controller, blockId } = ctx;

useEffect(() => {
cursorUpdateHandler.current = (evt: CursorStateUpdateEvent) => {
Expand All @@ -43,24 +39,23 @@ export function useBlockActive(props: BlockActiveDetectorProps): boolean {
return active;
}

export function useCollaborativeOutlineColor(
props: BlockActiveDetectorProps
): string | undefined {
export function useCollaborativeOutlineColor(): string | undefined {
const ctx = useContext(ReactBlockContext)!;
const [collaborativeOutlineColor, setCollaborativeOutlineColor] = useState<
string | undefined
>(undefined);
const { controller, blockId } = props;
const { editorController: controller, blockId } = ctx;
const applyCursorChangedEventHandler = useRef<
((e: CursorChangedEvent) => void) | undefined
>(undefined);

useEffect(() => {
applyCursorChangedEventHandler.current = (evt: CursorChangedEvent) => {
const { state } = evt;
const { editorController: controller, blockId } = ctx;
const shouldShowOutline =
state !== null && state.isCollapsed && state.id === props.blockId;
state !== null && state.isCollapsed && state.id === blockId;

const { controller } = props;
const { editor } = controller;
if (!editor) {
return;
Expand Down
Loading
Loading