Skip to content
Draft
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
4 changes: 2 additions & 2 deletions src/plugin/fieldViews/FieldView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export enum FieldContentType {
/**
* Represents a prosemirror-element view of a Prosemirror Node.
*/
export abstract class FieldView<NodeValue> {
export abstract class FieldView<FieldValue> {
public static fieldType: string;
public static fieldContentType: FieldContentType;
// The HTML element this fieldView renders content into.
Expand All @@ -53,7 +53,7 @@ export abstract class FieldView<NodeValue> {
/**
* Programmatically update this fieldView with the given value.
*/
public abstract update(value: NodeValue): void;
public abstract update(value: FieldValue): void;

/**
* Destroy this fieldView, cleaning up any resources it has instantiated.
Expand Down
19 changes: 13 additions & 6 deletions src/plugin/fieldViews/RepeaterFieldView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface RepeaterFieldDescription<
/**
* A FieldView representing a node that contains user-defined child nodes.
*/
export class RepeaterFieldView extends FieldView<unknown> {
export class RepeaterFieldView<ChildValue> extends FieldView<ChildValue> {
public static fieldType = repeaterFieldType;
public static fieldContentType = FieldContentType.REPEATER;
public static defaultValue = [];
Expand All @@ -48,7 +48,8 @@ export class RepeaterFieldView extends FieldView<unknown> {
// The outer editor instance. Updated from within this class when nodes are added or removed.
private outerView: EditorView,
private fieldName: string,
public minChildren: number
public minChildren: number,
private getChildNodeFromData: (data: ChildValue) => Node
) {
super();
}
Expand Down Expand Up @@ -93,9 +94,10 @@ export class RepeaterFieldView extends FieldView<unknown> {

/**
* Add a new child from this repeater at the given index.
* You can pre-populate this with
* If list is empty, you will need to pass -1.
*/
public addChildAfter(index: number) {
public addChildAfter(index: number, childValue?: ChildValue) {
if (index < -1 || index > this.node.childCount - 1) {
console.error(
`Cannot add at index ${index}: index out of range. Minimum -1, Maximum ${
Expand All @@ -108,9 +110,14 @@ export class RepeaterFieldView extends FieldView<unknown> {
const repeaterChildNodeName = getRepeaterChildNameFromParent(
this.node.type.name
);
const maybeNode = childValue
? this.getChildNodeFromData(childValue)
: undefined;

const newNode = this.node.type.schema.nodes[
repeaterChildNodeName
].createAndFill({ [RepeaterFieldMapIDKey]: getRepeaterID() });
].createAndFill({ [RepeaterFieldMapIDKey]: getRepeaterID() }, maybeNode);

if (!newNode) {
console.warn(
`[prosemirror-elements]: Could not create new repeater node of type ${this.fieldName}: createAndFill did not return a node`
Expand All @@ -137,8 +144,8 @@ export class RepeaterFieldView extends FieldView<unknown> {
this.outerView.dispatch(tr);
}

public addChildAtEnd() {
this.addChildAfter(this.node.childCount - 1);
public addChildAtEnd(childValue?: ChildValue) {
this.addChildAfter(this.node.childCount - 1, childValue);
}

/**
Expand Down
29 changes: 19 additions & 10 deletions src/plugin/helpers/fieldView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { TextFieldView } from "../fieldViews/TextFieldView";
import type { FieldDescription, FieldDescriptions } from "../types/Element";
import type { ExternalElementData } from "./element";
import type { KeysWithValsOfType, Optional } from "./types";
import { createNodesForFieldValues } from "../nodeSpec";

export const fieldTypeToViewMap = {
[TextFieldView.fieldType]: TextFieldView,
Expand Down Expand Up @@ -97,21 +98,21 @@ type Options = {

export const getElementFieldViewFromType = (
fieldName: string,
field: FieldDescription,
fieldDescription: FieldDescription,
{ node, view, getPos, offset, innerDecos }: Options
) => {
switch (field.type) {
switch (fieldDescription.type) {
case "text":
return new TextFieldView(node, view, getPos, offset, innerDecos, field);
return new TextFieldView(node, view, getPos, offset, innerDecos, fieldDescription);
case "nestedElement":
return new NestedElementFieldView(
node,
view,
getPos,
offset,
innerDecos,
field,
field.allowedPlugins
fieldDescription,
fieldDescription.allowedPlugins
);
case "richText":
return new RichTextFieldView(
Expand All @@ -120,15 +121,15 @@ export const getElementFieldViewFromType = (
getPos,
offset,
innerDecos,
field
fieldDescription
);
case "checkbox":
return new CheckboxFieldView(
node,
view,
getPos,
offset,
field.defaultValue ?? CheckboxFieldView.defaultValue
fieldDescription.defaultValue ?? CheckboxFieldView.defaultValue
);
case "custom":
return new CustomFieldView(node, view, getPos, offset);
Expand All @@ -138,17 +139,25 @@ export const getElementFieldViewFromType = (
view,
getPos,
offset,
field.defaultValue ?? DropdownFieldView.defaultValue,
field.options
fieldDescription.defaultValue ?? DropdownFieldView.defaultValue,
fieldDescription.options
);
case "repeater":
const a = (childValue: unknown) => createNodesForFieldValues(
node.type.schema, // got
fieldDescription, // need (parent has)
childValue, // got
nodeName, // ? not sure what 'node' means here
getNodeFromElementData, // will need (parent doesn't appear to have)
);
return new RepeaterFieldView(
node,
offset,
getPos,
view,
fieldName,
field.minChildren
fieldDescription.minChildren,
a
);
}
};
Loading