diff options
| author | geireann <geireann.lindfield@gmail.com> | 2021-08-17 21:11:58 -0400 | 
|---|---|---|
| committer | geireann <geireann.lindfield@gmail.com> | 2021-08-17 21:11:58 -0400 | 
| commit | 3b820202841a586506604db776e73a6cdc8d4015 (patch) | |
| tree | a8c1236cf6d686dd1b197e11f7bbd6f24f8f89e2 /src/client/views/nodes/formattedText/ProsemirrorExampleTransfer.ts | |
| parent | a634580c224c4113b6c8a4b83f17532e86b33225 (diff) | |
| parent | 412ec3b38b2ee396b2709d824f02b0e417f5d967 (diff) | |
Merge branch 'master' into search-david
Diffstat (limited to 'src/client/views/nodes/formattedText/ProsemirrorExampleTransfer.ts')
| -rw-r--r-- | src/client/views/nodes/formattedText/ProsemirrorExampleTransfer.ts | 73 | 
1 files changed, 47 insertions, 26 deletions
| diff --git a/src/client/views/nodes/formattedText/ProsemirrorExampleTransfer.ts b/src/client/views/nodes/formattedText/ProsemirrorExampleTransfer.ts index d5c77786c..eff400a98 100644 --- a/src/client/views/nodes/formattedText/ProsemirrorExampleTransfer.ts +++ b/src/client/views/nodes/formattedText/ProsemirrorExampleTransfer.ts @@ -7,13 +7,14 @@ import { splitListItem, wrapInList, } from "prosemirror-schema-list";  import { EditorState, Transaction, TextSelection } from "prosemirror-state";  import { SelectionManager } from "../../../util/SelectionManager";  import { NumCast, BoolCast, Cast, StrCast } from "../../../../fields/Types"; -import { Doc, DataSym, DocListCast } from "../../../../fields/Doc"; +import { Doc, DataSym, DocListCast, AclAugment, AclSelfEdit } from "../../../../fields/Doc";  import { FormattedTextBox } from "./FormattedTextBox";  import { Id } from "../../../../fields/FieldSymbols";  import { Docs } from "../../../documents/Documents";  import { Utils } from "../../../../Utils";  import { listSpec } from "../../../../fields/Schema";  import { List } from "../../../../fields/List"; +import { GetEffectiveAcl } from "../../../../fields/util";  const mac = typeof navigator !== "undefined" ? /Mac/.test(navigator.platform) : false; @@ -70,25 +71,42 @@ export function buildKeymap<S extends Schema<any>>(schema: S, props: any, mapKey          return false;      }; +    const canEdit = (state: any) => { +        switch (GetEffectiveAcl(props.Document)) { +            case AclAugment: return false; +            case AclSelfEdit: +                for (var i = state.selection.from; i < state.selection.to; i++) { +                    const marks = state.doc.resolve(i)?.marks?.(); +                    if (marks?.some((mark: any) => mark.type === schema.marks.user_mark && mark.attrs.userid !== Doc.CurrentUserEmail)) { +                        return false; +                    } +                } +                break; +        } +        return true; +    } + +    const toggleEditableMark = (mark: any) => (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && toggleMark(mark)(state, dispatch); +      //History commands      bind("Mod-z", undo);      bind("Shift-Mod-z", redo);      !mac && bind("Mod-y", redo);      //Commands to modify Mark -    bind("Mod-b", toggleMark(schema.marks.strong)); -    bind("Mod-B", toggleMark(schema.marks.strong)); +    bind("Mod-b", toggleEditableMark(schema.marks.strong)); +    bind("Mod-B", toggleEditableMark(schema.marks.strong)); -    bind("Mod-e", toggleMark(schema.marks.em)); -    bind("Mod-E", toggleMark(schema.marks.em)); +    bind("Mod-e", toggleEditableMark(schema.marks.em)); +    bind("Mod-E", toggleEditableMark(schema.marks.em)); -    bind("Mod-*", toggleMark(schema.marks.code)); +    bind("Mod-*", toggleEditableMark(schema.marks.code)); -    bind("Mod-u", toggleMark(schema.marks.underline)); -    bind("Mod-U", toggleMark(schema.marks.underline)); +    bind("Mod-u", toggleEditableMark(schema.marks.underline)); +    bind("Mod-U", toggleEditableMark(schema.marks.underline));      //Commands for lists -    bind("Ctrl-i", wrapInList(schema.nodes.ordered_list)); +    bind("Ctrl-i", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && wrapInList(schema.nodes.ordered_list)(state, dispatch as any));      bind("Tab", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => {          /// bcz; Argh!!  replace layotuTEmpalteString with a onTab prop conditionally handles Tab); @@ -96,6 +114,7 @@ export function buildKeymap<S extends Schema<any>>(schema: S, props: any, mapKey              if (!props.LayoutTemplateString) return addTextBox(false, true);              return true;          } +        if (!canEdit(state)) return true;          const ref = state.selection;          const range = ref.$from.blockRange(ref.$to);          const marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks()); @@ -121,6 +140,7 @@ export function buildKeymap<S extends Schema<any>>(schema: S, props: any, mapKey      bind("Shift-Tab", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => {          /// bcz; Argh!!  replace with a onShiftTab prop conditionally handles Tab);          if (props.Document._singleLine) return true; +        if (!canEdit(state)) return true;          const marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks());          if (!liftListItem(schema.nodes.list_item)(state.tr, (tx2: Transaction) => { @@ -140,24 +160,19 @@ export function buildKeymap<S extends Schema<any>>(schema: S, props: any, mapKey      });      //Commands to modify BlockType -    bind("Ctrl->", wrapIn(schema.nodes.blockquote)); -    bind("Alt-\\", setBlockType(schema.nodes.paragraph)); -    bind("Shift-Ctrl-\\", setBlockType(schema.nodes.code_block)); +    bind("Ctrl->", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit((state) && wrapIn(schema.nodes.blockquote)(state, dispatch as any))); +    bind("Alt-\\", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && setBlockType(schema.nodes.paragraph)(state, dispatch as any)); +    bind("Shift-Ctrl-\\", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && setBlockType(schema.nodes.code_block)(state, dispatch as any)); -    bind("Ctrl-m", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => { -        dispatch(state.tr.replaceSelectionWith(schema.nodes.equation.create({ fieldKey: "math" + Utils.GenerateGuid() }))); -    }); +    bind("Ctrl-m", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && dispatch(state.tr.replaceSelectionWith(schema.nodes.equation.create({ fieldKey: "math" + Utils.GenerateGuid() }))));      for (let i = 1; i <= 6; i++) { -        bind("Shift-Ctrl-" + i, setBlockType(schema.nodes.heading, { level: i })); +        bind("Shift-Ctrl-" + i, (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && setBlockType(schema.nodes.heading, { level: i })(state, dispatch as any));      }      //Command to create a horizontal break line      const hr = schema.nodes.horizontal_rule; -    bind("Mod-_", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => { -        dispatch(state.tr.replaceSelectionWith(hr.create()).scrollIntoView()); -        return true; -    }); +    bind("Mod-_", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && dispatch(state.tr.replaceSelectionWith(hr.create()).scrollIntoView()));      //Command to unselect all      bind("Escape", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => { @@ -173,13 +188,15 @@ export function buildKeymap<S extends Schema<any>>(schema: S, props: any, mapKey      };      //Command to create a text document to the right of the selected textbox -    bind("Alt-Enter", (state: EditorState<S>, dispatch: (tx: Transaction<Schema<any, any>>) => void) => addTextBox(false, true)); +    bind("Alt-Enter", () => addTextBox(false, true));      //Command to create a text document to the bottom of the selected textbox -    bind("Ctrl-Enter", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => addTextBox(true, true)); +    bind("Ctrl-Enter", () => addTextBox(true, true));      // backspace = chainCommands(deleteSelection, joinBackward, selectNodeBackward);      bind("Backspace", (state: EditorState<S>, dispatch: (tx: Transaction<Schema<any, any>>) => void) => { +        if (!canEdit(state)) return true; +          if (!deleteSelection(state, (tx: Transaction<Schema<any, any>>) => {              dispatch(updateBullets(tx, schema));          })) { @@ -200,6 +217,9 @@ export function buildKeymap<S extends Schema<any>>(schema: S, props: any, mapKey      //command to break line      bind("Enter", (state: EditorState<S>, dispatch: (tx: Transaction<Schema<any, any>>) => void) => {          if (addTextBox(true, false)) return true; + +        if (!canEdit(state)) return true; +          const trange = state.selection.$from.blockRange(state.selection.$to);          const path = (state.selection.$from as any).path;          const depth = trange ? liftTarget(trange) : undefined; @@ -238,18 +258,19 @@ export function buildKeymap<S extends Schema<any>>(schema: S, props: any, mapKey      //Command to create a blank space      bind("Space", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => { +        if (!canEdit(state)) return true;          const marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks());          dispatch(splitMetadata(marks, state.tr));          return false;      }); -    bind("Alt-ArrowUp", joinUp); -    bind("Alt-ArrowDown", joinDown); -    bind("Mod-BracketLeft", lift); +    bind("Alt-ArrowUp", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && joinUp(state, dispatch as any)); +    bind("Alt-ArrowDown", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && joinDown(state, dispatch as any)); +    bind("Mod-BracketLeft", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => canEdit(state) && lift(state, dispatch as any));      const cmd = chainCommands(exitCode, (state, dispatch) => {          if (dispatch) { -            dispatch(state.tr.replaceSelectionWith(schema.nodes.hard_break.create()).scrollIntoView()); +            canEdit(state) && dispatch(state.tr.replaceSelectionWith(schema.nodes.hard_break.create()).scrollIntoView());              return true;          }          return false; | 
