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

docs: add pasteImage to demo and document the event #426

Merged
merged 1 commit into from
Mar 20, 2023
Merged
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
19 changes: 18 additions & 1 deletion Demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<body>
<h1>Squire Editor Demo</h1>
<header>
<p>Squire is a rich text editor primarily built for email apps. It’s designed to be integrated with your own UI framework, and so does not provide its own UI toolbar, widgets or overlays. This is a really simple demo, with the most trivial of UI integrations, to show the raw component in action. <a href="https://github.com/neilj/squire">Learn more and see the source on GitHub</a>.</p>
<p>Squire is a rich text editor primarily built for email apps. It’s designed to be integrated with your own UI framework, and so does not provide its own UI toolbar, widgets or overlays. This is a really simple demo, with the most trivial of UI integrations, to show the raw component in action. <a href="https://github.com/fastmail/squire">Learn more and see the source on GitHub</a>.</p>
<p>
<span id="bold">Bold</span>
<span id="removeBold">Unbold</span>
Expand Down Expand Up @@ -135,6 +135,23 @@ <h1>Squire Editor Demo</h1>
}
});

editor.addEventListener('pasteImage', function(event) {
const items = [...event.detail.clipboardData.items];
const imageItems = items.filter((item) => /image/.test(item.type));

if (!imageItems.length) {
return false;
}

let reader = new FileReader();

reader.onload = (loadEvent) => {
this.insertImage(loadEvent.target.result);
}

reader.readAsDataURL(imageItems[0].getAsFile());
});

document.addEventListener( 'click', function ( e ) {
var id = e.target.id,
value;
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Squire is an HTML5 rich text editor, which provides powerful cross-browser norma

It was designed to handle email composition for the [Fastmail](https://www.fastmail.com) web app. The most important consequence of this (and where Squire differs from most other modern rich text editors) is that it must handle arbitrary HTML, because it may be used to forward or quote emails from third-parties and must be able to preserve their HTML without breaking the formatting. This means that it can't use a more structured (but limited) internal data model (as most other modern HTML editors do) and the HTML remains the source-of-truth. The other consequence is excellent handling of multiple levels of blockquotes.

Squire is designed to be integrated with your own UI framework, and so does not provide its own UI toolbar, widgets or overlays. Instead, you get a component you can insert in place of a `<textarea>` and manipulate programatically, allowing you to integrate seamlessly with the rest of your application and lose the bloat of having two UI toolkits loaded.
Squire is designed to be integrated with your own UI framework, and so does not provide its own UI toolbar, widgets or overlays. Instead, you get a component you can insert in place of a `<textarea>` and manipulate programmatically, allowing you to integrate seamlessly with the rest of your application and lose the bloat of having two UI toolkits loaded.

Squire supports all reasonably recent browsers. It no longer supports any version of IE.

Expand Down Expand Up @@ -84,10 +84,10 @@ Attach an event listener to the editor. The handler can be either a function or
- **input**: The user inserted, deleted or changed the style of some text; in other words, the result for `editor.getHTML()` will have changed.
- **pathChange**: The path (see getPath documentation) to the cursor has changed. The new path is available as the `path` property on the event's `detail` property object.
- **select**: The user selected some text.
- **cursor**: The user cleared their selection or moved the cursor to a
different position.
- **cursor**: The user cleared their selection or moved the cursor to a different position.
- **undoStateChange**: The availability of undo and/or redo has changed. The event object has a `detail` property, which is an object with two boolean properties, `canUndo` and `canRedo` to let you know the new state.
- **willPaste**: The user is pasting content into the document. The content that will be inserted is available as either the `fragment` property, or the `text` property for plain text, on the `detail` property of the event. You can modify this text/fragment in your event handler to change what will be pasted. You can also call the `preventDefault` on the event object to cancel the paste operation.
- **pasteImage**: The user is pasting image content into the document.

The method takes two arguments:

Expand Down
10 changes: 2 additions & 8 deletions dist/squire-raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,18 +466,14 @@
return node;
};
var fixContainer = (container, root) => {
const children = container.childNodes;
let wrapper = null;
for (let i = 0, l = children.length; i < l; i += 1) {
const child = children[i];
[...container.childNodes].forEach((child) => {
const isBR = child.nodeName === "BR";
if (!isBR && isInline(child)) {
if (!wrapper) {
wrapper = createElement("DIV");
}
wrapper.appendChild(child);
i -= 1;
l -= 1;
} else if (isBR || wrapper) {
if (!wrapper) {
wrapper = createElement("DIV");
Expand All @@ -487,15 +483,13 @@
container.replaceChild(wrapper, child);
} else {
container.insertBefore(wrapper, child);
i += 1;
l += 1;
}
wrapper = null;
}
if (isContainer(child)) {
fixContainer(child, root);
}
}
});
if (wrapper) {
container.appendChild(fixCursor(wrapper));
}
Expand Down
10 changes: 2 additions & 8 deletions dist/squire-raw.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -468,18 +468,14 @@ var fixCursor = (node) => {
return node;
};
var fixContainer = (container, root) => {
const children = container.childNodes;
let wrapper = null;
for (let i = 0, l = children.length; i < l; i += 1) {
const child = children[i];
[...container.childNodes].forEach((child) => {
const isBR = child.nodeName === "BR";
if (!isBR && isInline(child)) {
if (!wrapper) {
wrapper = createElement("DIV");
}
wrapper.appendChild(child);
i -= 1;
l -= 1;
} else if (isBR || wrapper) {
if (!wrapper) {
wrapper = createElement("DIV");
Expand All @@ -489,15 +485,13 @@ var fixContainer = (container, root) => {
container.replaceChild(wrapper, child);
} else {
container.insertBefore(wrapper, child);
i += 1;
l += 1;
}
wrapper = null;
}
if (isContainer(child)) {
fixContainer(child, root);
}
}
});
if (wrapper) {
container.appendChild(fixCursor(wrapper));
}
Expand Down
Loading