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

Move some files to typescript #264

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ export function extensionFactory(options) {
atom: options.atom || false,
addOptions: options.options || {},
// priority: options.priority || 1, // somehow this option breaks the addKeyboardShortcut
onBeforeCreate({ editor }) {
onBeforeCreate() {
// Before the view is created.
options.onBeforeCreate && options.onBeforeCreate(editor);
options.onBeforeCreate && options.onBeforeCreate(this.editor);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is needed this.editor? is this working?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are actually two ways to do this because it looks like tiptap both passes the object in and binds the function to it.

So this:

methodName({ editor }) {
    doThingWith(editor)
}

Can become:

methodName() {
    doThingWith(this.editor)
}

Or this:

methodName(this: { editor }) {
    doThingWith(editor)
}

But if you do the second one, it seems like it no longer recognizes the type of the editor argument for some reason.

Given that these functions are being bound to the object within tiptap and their this argument is very well typed, I assume this works. It also matches their documentation. I did some minimal testing to make sure I didn't massively break anything, but I'm sure it needs more attention.

},
onCreate({ editor }) {
onCreate() {
// The editor is ready.
options.onCreate && options.onCreate(editor);
options.onCreate && options.onCreate(this.editor);
},
onUpdate({ editor }) {
onUpdate() {
// The content has changed.
options.onUpdate && options.onUpdate(editor);
options.onUpdate && options.onUpdate(this.editor);
},
onSelectionUpdate({ editor }) {
onSelectionUpdate() {
// The selection has changed.
options.onSelectionUpdate && options.onSelectionUpdate(editor);
options.onSelectionUpdate && options.onSelectionUpdate(this.editor);
},
onTransaction({ editor, transaction }) {
onTransaction() {
// The editor state has changed.
options.onTransaction && options.onTransaction(editor);
options.onTransaction && options.onTransaction(this.editor);
},
onFocus({ editor, event }) {
onFocus() {
// The editor is focused.
options.onFocus && options.onFocus(editor);
options.onFocus && options.onFocus(this.editor);
},
onBlur({ editor, event }) {
onBlur() {
// The editor isn’t focused anymore.
options.onBlur && options.onBlur(editor);
options.onBlur && options.onBlur(this.editor);
},
onDestroy() {
// The editor is being destroyed.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { Extension } from "@tiptap/core";
import "@tiptap/extension-text-style";

// Recommended approach for extending commands
// @see https://tiptap.dev/guide/typescript#command-type
declare module '@tiptap/core' {
interface Commands<ReturnType> {
customExtension: {
setColor: (color: string) => ReturnType,
unsetColor: () => ReturnType
}
}
}

export const Color = Extension.create({
name: "color",

addOptions: {
types: ["textStyle"],
addOptions() {
return {
types: ["textStyle"],
}
},

addGlobalAttributes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { Plugin } from "prosemirror-state";
export const Placeholder = Extension.create({
name: "placeholder",

addOptions: {
emptyEditorClass: "is-editor-empty",
emptyNodeClass: "is-empty",
placeholder: "Write something …",
showOnlyWhenEditable: true,
showOnlyCurrent: true,
addOptions() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this converted into a function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has something to do with the internals of tiptap's type inference. It's how they recommend doing it in the typescript compatibility section of their docs and it was causing an error to leave it as-is

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, 👌

return {
emptyEditorClass: "is-editor-empty",
emptyNodeClass: "is-empty",
placeholder: "Write something …",
showOnlyWhenEditable: true,
showOnlyCurrent: true,
}
},

addProseMirrorPlugins() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default function DanteTooltipColor(props) {
} else {
return
}*/
return undefined
}

function renderColor() {
Expand All @@ -48,7 +49,7 @@ export default function DanteTooltipColor(props) {
>
<HexColorPicker
color={v}
onChange={(color, e) => {
onChange={color => {
setValue(color);
//handleChange(e, color);
//this.handleClick(e, color )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { CSSProperties } from "react";
import { AnchorStyle } from "../styled/menu";
import ImagePopover from "./image";
import { BubbleMenu } from "./bubble-menu/bubble-menu-react";
Expand Down Expand Up @@ -40,8 +40,16 @@ function DanteTooltipLink({ enableLinkMode, selected }) {
);
}

interface LinkState {
link_mode: boolean
url?: string
menu_style: {
minWidth?: string,
}
}

export default function MenuBar({ editor, fixed }) {
const [linkState, setLinkState] = useState({
const [linkState, setLinkState] = useState<LinkState>({
link_mode: false,
menu_style: {
minWidth: "200px",
Expand All @@ -63,7 +71,7 @@ export default function MenuBar({ editor, fixed }) {
}

function displayActiveMenu() {
if (this.state.show) {
if (show) {
return "dante-menu--active";
} else {
return "";
Expand Down Expand Up @@ -117,7 +125,7 @@ export default function MenuBar({ editor, fixed }) {
editor.chain().focus().setColor(style).run();
}

function fixedStyles() {
function fixedStyles(): CSSProperties {
if (!fixed) return { width: `${11 * 43}px` };
if (fixed) return { position: `sticky`, top: "0" };
}
Expand All @@ -128,7 +136,7 @@ export default function MenuBar({ editor, fixed }) {

return (
<AnchorStyle
fixed={fixed}
//fixed={fixed} // This wasn't present in the type for AnchorStyle and didn't appear to be referenced anywhere
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets keep this for now

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be able to add fixed as a prop to AnchorStyle, but I'm still trying to figure out how to work with the types on those styled components

className={`dante-menu ${displayLinkMode()}`}
style={fixedStyles()}
>
Expand Down Expand Up @@ -171,7 +179,7 @@ export default function MenuBar({ editor, fixed }) {

<DanteTooltipLink
selected={editor.isActive("link")}
editor={editor}
//editor={editor}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep this from now

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. I'll add the editor into the tooltip props instead.

enableLinkMode={_enableLinkMode}
/>
<li
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const AnchorStyle = styled.div`
height: 0;
width: 0;
position: absolute;
left: ${(props) => (props.arrowPosition ? props.arrowPosition : "50%")};
left: ${(props) => (props['arrowPosition'] ? props['arrowPosition'] : "50%")};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this change needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That property wasn't showing up on the props type signature, but the type was indexable by arbitrary strings. I can try typing the styled div with a <{arrowPosition: string}> generic and see if that works. It would certainly be less hacky.

pointer-events: none;
border: ${(props) => props.theme.dante_menu_caret_size} solid transparent;
margin-left: -${(props) => math(`${props.theme.dante_menu_caret_size} / 2`)};
Expand Down
88 changes: 88 additions & 0 deletions packages/dante3/src/styled/themes/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import '@emotion/react'

// This is the prescribed method for using custom themes with emotion and typescript
// @see https://emotion.sh/docs/typescript
declare module '@emotion/react' {
export interface Theme {
dante_font_family_serif: string
dante_font_family_sans: string
dante_font_family_mono: string
dante_font_family_base: string

// Editor
dante_editor_font_size: string
dante_editor_line_height: string

//dante_font_family_sans_serif: "comic-sans",
dante_visual_debugger: string
dante_text_color: string
dante_inversed_color: string
dante_accent_color: string
dante_control_color: string
dante_popover_color: string

//dante_font_size_base: '24px',
//line_height_base: '1.428571429', // 20/14

tooltip_color: string
tooltip_background_color: string
tooltip_border_color: string
tooltip_color_opacity: string
tooltip_color_opacity_hover: string
tooltip_background_opacity: string
tooltip_border_width: string
tooltip_border_radius: string

tooltip_caret_size: string
menu_tone: string

tooltip_button_spacing: string
tooltip_menu_spacing: string

tooltip_items: number // Fix this and remove it
tooltip_item_delay: number
tooltip_size: string
tooltip_line_height: string

tooltip_default_transition: string
tooltip_forward_transition: string
tooltip_backward_transition: string

dante_code_background: string
dante_code_color: string

// Menu

//background: #2A2B32;

dante_menu_height: string
dante_menu_background: string
dante_menu_color: string
dante_menu_border_radius: string
dante_menu_box_shadow: string
dante_menu_icon_size: string
dante_menu_icon_color: string
dante_menu_icon_accent: string
dante_menu_divider_color: string
dante_menu_border_width: string
dante_menu_border_color: string
dante_menu_caret_size: string
dante_bg_color: string

// highlight theme
// highlight theme
// find other themes at https://highlightjs.org/static/demo/
// https://github.com/highlightjs/highlight.js/tree/main/src/styles
hljs_color: string
hljs_background: string
hljs_emphasis_color: string
hljs_literal_color: string
hljs_selector_class_color: string
hljs_name_color: string
hljs_title_color: string
hljs_variable_color: string
hljs_class_title_color: string
hljs_link_color: string
hljs_deletion_color: string
}
}
File renamed without changes.