Skip to content
Snippets Groups Projects
Commit 0c7dbfab authored by Giannis Kopanas's avatar Giannis Kopanas
Browse files

feat(overlay): create overlay component

parent fd588f70
No related branches found
No related tags found
1 merge request!45Develop
...@@ -12,8 +12,6 @@ export default class LinkService extends Service { ...@@ -12,8 +12,6 @@ export default class LinkService extends Service {
name = "LinkPlugin"; name = "LinkPlugin";
boot() { boot() {
// this.app.PmPlugins.add(PLUGIN_KEY, LinkPlugin(PLUGIN_KEY));
const createOverlay = this.container.get("CreateOverlay"); const createOverlay = this.container.get("CreateOverlay");
createOverlay(LinkComponent); createOverlay(LinkComponent);
} }
......
import { Plugin, PluginKey } from "prosemirror-state";
export default keyPlugin => {
const plugin_key = new PluginKey(keyPlugin);
return new Plugin({
key: plugin_key,
state: {
init: function init() {
return {
action: "HIDE_OVERLAY"
};
},
apply(tr, oldState, newState, test) {
const { key } = plugin_key;
newState[key].action = "OPEN_OVERLAY";
// console.log("first time");
// newState[key].action =
// newState[key].action === "OPEN_OVERLAY"
// ? "HIDE_OVERLAY"
// : "OPEN_OVERLAY";
return this.getState(newState);
}
}
});
};
import React, { useMemo } from "react"; import React from "react";
export default () => { export default Component => () => {
return <div style={{ width: "100px", height: "100px" }}>Overlay Area</div>; return (
<div style={{ width: "100px", height: "100px" }}>
Overlay Area
<Component />
</div>
);
}; };
...@@ -8,12 +8,11 @@ export default class OverlayService extends Service { ...@@ -8,12 +8,11 @@ export default class OverlayService extends Service {
boot() {} boot() {}
register() { register() {
const Components = [];
this.container.bind("CreateOverlay").toFactory(context => { this.container.bind("CreateOverlay").toFactory(context => {
return Component => { return Component => {
const PmPlugins = context.container.get("PmPlugins"); const layout = context.container.get("Layout");
Components.push(Component); debugger;
//PmPlugins.add(PLUGIN_KEY, OverlayPlugin(Components)); layout.addComponent("waxOverlays", OverlayComponent(Component));
}; };
}); });
} }
......
import React from "react";
import { render as renderReact } from "react-dom";
import OverlayComponent from "../OverlayComponent";
export default class Overlay {
constructor(view) {
this.tooltip = document.createElement("div");
this.tooltip.className = "fds";
console.log(view.dom);
view.dom.parentNode.appendChild(this.tooltip);
this.update(view, null);
}
render(view) {
const { state } = view;
const { doc, selection, schema } = state;
const markType = schema.marks.link;
if (!markType) return;
const { from, to, empty } = view.state.selection;
const start = view.coordsAtPos(from);
const end = view.coordsAtPos(to);
const box = 0; //this.tooltip.getBoundingClientRect();
const left = Math.max((start.left + end.left) / 2, start.left + 3);
const leftSpacing = `${left - box}px`;
const bottomSpacing = `${box - start.top}px`;
const numberOfCharactersInSelection = to - from;
return (
<OverlayComponent
display={empty ? "none" : undefined}
left={leftSpacing}
bottom={bottomSpacing}
>
{numberOfCharactersInSelection}
</OverlayComponent>
);
}
update(view, lastState) {
const { state } = view;
const { doc, selection, schema } = state;
const markType = schema.marks.link;
if (!markType) return;
const { from, to } = selection;
const domFound = view.domAtPos(from);
if (!domFound) {
this.destroy();
return;
}
// debugger;
// const anchorEl = lookUpElement(domFound.node, el => el.nodeName === "A");
// if (!anchorEl) {
// this.destroy();
// return;
// }
//if (areStatesEqual(state, lastState)) return;
// Don't do anything if the document/selection didn't change
if (
lastState &&
lastState.doc.eq(state.doc) &&
lastState.selection.eq(state.selection)
)
return;
//const tooltipComponent = this.render(view);
console.log(view.dom);
//renderReact(tooltipComponent, view.dom);
}
destroy() {
//this.tooltip.remove();
}
}
import { Plugin, PluginKey } from "prosemirror-state";
import Overlay from "./Overlay";
export default Components =>
new Plugin({
key: new PluginKey("test"),
view(editorView) {
return new Overlay(editorView);
}
});
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment