Skip to content

Commit

Permalink
fix select all and change ordinal in edit mode in io (#3109)
Browse files Browse the repository at this point in the history
* fix select all and change ordinal in edit mode in io

* make ordinal undefined for all shapes in group/ungroup

* fix group shapes and some ui fixes

* Don't add node_modules/* to dprint deps

* use minimum ordinal when shape merged, use max ordinal++ when ungrouped, in add mode no ordinal preset so NaN

* use state for ungroup shape

* maintain existing ordinal in editing mode

* fix order of ordinal in ungroup shape

* refactor: remove for loop, use forEach

(cherry picked from commit 477f932)
  • Loading branch information
dae committed Apr 11, 2024
1 parent b8e7dd0 commit 6643b1b
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 34 deletions.
2 changes: 1 addition & 1 deletion build/configure/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn build_and_check_reviewer(build: &mut Build) -> Result<()> {
fn check_web(build: &mut Build) -> Result<()> {
let dprint_files = inputs![glob![
"**/*.{ts,mjs,js,md,json,toml,svelte,scss}",
"target/**"
"{target,ts/.svelte-kit,node_modules}/**"
]];
build.add_action(
"check:format:dprint",
Expand Down
2 changes: 1 addition & 1 deletion ftl/core-repo
Submodule core-repo updated 66 files
+1 −1 core/ar/actions.ftl
+12 −6 core/ar/deck-config.ftl
+17 −12 core/cs/deck-config.ftl
+2 −2 core/cs/editing.ftl
+6 −6 core/cs/exporting.ftl
+4 −4 core/cs/importing.ftl
+8 −6 core/el/deck-config.ftl
+13 −3 core/fi/deck-config.ftl
+6 −0 core/fi/importing.ftl
+2 −9 core/fi/scheduling.ftl
+1 −1 core/fr/actions.ftl
+2 −2 core/fr/browsing.ftl
+61 −9 core/fr/deck-config.ftl
+5 −2 core/fr/decks.ftl
+22 −1 core/fr/editing.ftl
+4 −3 core/fr/exporting.ftl
+1 −0 core/fr/fields.ftl
+66 −0 core/fr/importing.ftl
+5 −3 core/fr/notetypes.ftl
+2 −1 core/fr/preferences.ftl
+2 −2 core/fr/scheduling.ftl
+31 −2 core/fr/statistics.ftl
+1 −0 core/fr/studying.ftl
+5 −4 core/fr/sync.ftl
+25 −12 core/he/deck-config.ftl
+7 −0 core/he/importing.ftl
+17 −9 core/it/deck-config.ftl
+5 −10 core/ja/deck-config.ftl
+1 −1 core/ja/importing.ftl
+1 −1 core/pl/actions.ftl
+1 −0 core/pl/adding.ftl
+1 −0 core/pl/card-stats.ftl
+1 −0 core/pl/card-templates.ftl
+16 −0 core/pl/deck-config.ftl
+5 −2 core/pl/decks.ftl
+6 −2 core/pl/errors.ftl
+1 −0 core/pl/fields.ftl
+5 −0 core/pl/help.ftl
+2 −0 core/pl/importing.ftl
+1 −0 core/pl/media-check.ftl
+9 −2 core/pl/notetypes.ftl
+2 −1 core/pl/preferences.ftl
+1 −1 core/pl/scheduling.ftl
+2 −0 core/pl/search.ftl
+1 −0 core/pl/studying.ftl
+2 −2 core/ru/deck-config.ftl
+7 −4 core/templates/deck-config.ftl
+2 −2 core/templates/scheduling.ftl
+8 −0 core/tr/browsing.ftl
+1 −0 core/tr/card-stats.ftl
+1 −1 core/tr/database-check.ftl
+1 −0 core/tr/deck-config.ftl
+4 −0 core/tr/errors.ftl
+2 −2 core/uk/custom-study.ftl
+96 −23 core/uk/deck-config.ftl
+5 −5 core/uk/scheduling.ftl
+11 −6 core/zh-CN/deck-config.ftl
+1 −1 core/zh-CN/importing.ftl
+2 −2 core/zh-CN/scheduling.ftl
+1 −1 core/zh-CN/studying.ftl
+1 −1 core/zh-TW/adding.ftl
+1 −1 core/zh-TW/database-check.ftl
+17 −10 core/zh-TW/deck-config.ftl
+1 −1 core/zh-TW/importing.ftl
+1 −1 core/zh-TW/preferences.ftl
+1 −1 core/zh-TW/scheduling.ftl
6 changes: 5 additions & 1 deletion ts/image-occlusion/Toolbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
font-size: 16px !important;
}
:global(.top-tool-icon-button:active) {
background: var(--highlight-bg) !important;
}
.dropdown-content {
display: none;
position: absolute;
Expand All @@ -479,7 +483,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
}
.show {
display: flex;
display: table;
}
::-webkit-scrollbar {
Expand Down
87 changes: 64 additions & 23 deletions ts/image-occlusion/shapes/to-cloze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,71 @@ export function exportShapesToClozeDeletions(occludeInactive: boolean): {
const shapes = baseShapesFromFabric();

let clozes = "";
let index = 0;
shapes.forEach((shapeOrShapes) => {
// shapes with width or height less than 5 are not valid
let noteCount = 0;

// take out all ordinal values from shapes
const ordinalList = shapes.map((shape) => {
if (Array.isArray(shape)) {
return shape[0].ordinal;
} else {
return shape.ordinal;
}
});

const filterOrdinalList: number[] = ordinalList.flatMap(v => typeof v === "number" ? [v] : []);
const maxOrdinal = Math.max(...filterOrdinalList, 0);

const missingOrdinals: number[] = [];
for (let i = 1; i <= maxOrdinal; i++) {
if (!ordinalList.includes(i)) {
missingOrdinals.push(i);
}
}

let nextOrdinal = maxOrdinal + 1;

shapes.map((shapeOrShapes) => {
if (shapeOrShapes === null) {
return;
}
// if shape is Rect and fill is transparent, skip it
if (shapeOrShapes instanceof Rectangle && shapeOrShapes.fill === "transparent") {
return;

// Maintain existing ordinal in editing mode
let ordinal: number | undefined;
if (Array.isArray(shapeOrShapes)) {
ordinal = shapeOrShapes[0].ordinal;
} else {
ordinal = shapeOrShapes.ordinal;
}
clozes += shapeOrShapesToCloze(shapeOrShapes, index, occludeInactive);

if (ordinal === undefined) {
// if ordinal is undefined, assign a missing ordinal if available
if (shapeOrShapes instanceof Text) {
ordinal = 0;
} else if (missingOrdinals.length > 0) {
ordinal = missingOrdinals.shift() as number;
} else {
ordinal = nextOrdinal;
nextOrdinal++;
}

if (Array.isArray(shapeOrShapes)) {
shapeOrShapes.forEach((shape) => (shape.ordinal = ordinal));
} else {
shapeOrShapes.ordinal = ordinal;
}
}

clozes += shapeOrShapesToCloze(
shapeOrShapes,
ordinal,
occludeInactive,
);

if (!(shapeOrShapes instanceof Text)) {
index++;
noteCount++;
}
});

return { clozes, noteCount: index };
return { clozes, noteCount };
}

/** Gather all Fabric shapes, and convert them into BaseShapes or
Expand All @@ -51,14 +99,17 @@ export function baseShapesFromFabric(): ShapeOrShapes[] {
: null;
const objects = canvas.getObjects() as FabricObject[];
const boundingBox = getBoundingBox();
// filter transparent rectangles
return objects
.map((object) => {
// If the object is in the active selection containing multiple objects,
// we need to calculate its x and y coordinates relative to the canvas.
const parent = selectionContainingMultipleObjects?.contains(object)
? selectionContainingMultipleObjects
: undefined;
if (object.width < 5 || object.height < 5) {
// shapes with width or height less than 5 are not valid
// if shape is Rect and fill is transparent, skip it
if (object.width! < 5 || object.height! < 5 || object.fill == "transparent") {
return null;
}
return fabricObjectToBaseShapeOrShapes(
Expand Down Expand Up @@ -132,7 +183,7 @@ function fabricObjectToBaseShapeOrShapes(
{{c1::image-occlusion:rect:top=.1:left=.23:width=.4:height=.5}} */
function shapeOrShapesToCloze(
shapeOrShapes: ShapeOrShapes,
index: number,
ordinal: number,
occludeInactive: boolean,
): string {
let text = "";
Expand All @@ -144,7 +195,7 @@ function shapeOrShapesToCloze(
let type: string;
if (Array.isArray(shapeOrShapes)) {
return shapeOrShapes
.map((shape) => shapeOrShapesToCloze(shape, index, occludeInactive))
.map((shape) => shapeOrShapesToCloze(shape, ordinal, occludeInactive))
.join("");
} else if (shapeOrShapes instanceof Rectangle) {
type = "rect";
Expand All @@ -165,16 +216,6 @@ function shapeOrShapesToCloze(
addKeyValue("oi", "1");
}

// Maintain existing ordinal in editing mode
let ordinal = shapeOrShapes.ordinal;
if (ordinal === undefined) {
if (type === "text") {
ordinal = 0;
} else {
ordinal = index + 1;
}
shapeOrShapes.ordinal = ordinal;
}
text = `{{c${ordinal}::image-occlusion:${type}${text}}}<br>`;

return text;
Expand Down
25 changes: 19 additions & 6 deletions ts/image-occlusion/tools/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@ export const groupShapes = (canvas: fabric.Canvas): void => {

const activeObject = canvas.getActiveObject();
const items = activeObject.getObjects();

let minOrdinal: number | undefined = Math.min(...items.map((item) => item.ordinal));
minOrdinal = Number.isNaN(minOrdinal) ? undefined : minOrdinal;

items.forEach((item) => {
item.set({ opacity: 1 });
item.set({ opacity: 1, ordinal: minOrdinal });
});

activeObject.toGroup().set({
opacity: get(opacityStateStore) ? 0.4 : 1,
});

redraw(canvas);
};

Expand All @@ -85,13 +91,16 @@ export const unGroupShapes = (canvas: fabric.Canvas): void => {
const items = group.getObjects();
group._restoreObjectsState();
group.destroyed = true;
canvas.remove(group);

items.forEach((item) => {
item.set({ opacity: get(opacityStateStore) ? 0.4 : 1 });
item.set({
opacity: get(opacityStateStore) ? 0.4 : 1,
ordinal: undefined,
});
canvas.add(item);
});

canvas.remove(group);
redraw(canvas);
};

Expand Down Expand Up @@ -282,9 +291,13 @@ export const makeShapeRemainInCanvas = (canvas: fabric.Canvas, boundingBox: fabr

export const selectAllShapes = (canvas: fabric.Canvas) => {
canvas.discardActiveObject();
const sel = new fabric.ActiveSelection(canvas.getObjects(), {
canvas: canvas,
});
// filter out the transparent bounding box from the selection
const sel = new fabric.ActiveSelection(
canvas.getObjects().filter((obj) => obj.fill !== "transparent"),
{
canvas: canvas,
},
);
canvas.setActiveObject(sel);
redraw(canvas);
};
Expand Down
8 changes: 7 additions & 1 deletion ts/image-occlusion/tools/tool-undo-redo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

import * as tr from "@tslib/ftl";
import type fabric from "fabric";
import fabric from "fabric";
import { writable } from "svelte/store";

import { mdiRedo, mdiUndo } from "../icons";
Expand Down Expand Up @@ -87,6 +87,12 @@ class UndoStack {
emitChangeSignal();
this.locked = false;
});
// make bounding box unselectable
this.canvas?.forEachObject((obj) => {
if (obj instanceof fabric.Rect && obj.fill === "transparent") {
obj.selectable = false;
}
});
}

onObjectAdded(id: string): void {
Expand Down

0 comments on commit 6643b1b

Please sign in to comment.