Skip to content

Commit

Permalink
feat: ✨ add iframe extension to insert embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Leecason committed Jan 1, 2020
1 parent c27d694 commit 290ac2c
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/components/ElementTiptap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,22 @@ export default {
width: 4px;
z-index: 20;
}
.iframe {
height: 0;
padding-bottom: 56.25%;
position: relative;
width: 100%;
&__embed {
border: 0;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
}
}
&__placeholder {
Expand Down
1 change: 1 addition & 0 deletions src/components/MenuCommands/CommandButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'vue-awesome/icons/italic';
import 'vue-awesome/icons/strikethrough';
import 'vue-awesome/icons/link';
import 'vue-awesome/icons/image';
import 'vue-awesome/icons/video';
import 'vue-awesome/icons/code';
import 'vue-awesome/icons/quote-right';
import 'vue-awesome/icons/align-left';
Expand Down
41 changes: 41 additions & 0 deletions src/components/MenuCommands/IframeCommandButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<template>
<command-button
:command="openInsertVideoControl"
tooltip="Insert Video"
icon="video"
/>
</template>

<script>
import CommandButton from './CommandButton.vue';
export default {
name: 'IframeCommandButton',
components: {
CommandButton,
},
props: {
editorContext: {
type: Object,
required: true,
},
},
methods: {
openInsertVideoControl () {
this.$prompt('', 'Insert Video', {
confirmButtonText: 'Insert',
cancelButtonText: 'Cancel',
inputPlaceholder: 'Href',
roundButton: true,
}).then(({ value: href }) => {
this.editorContext.commands.iframe({ src: href });
}).catch(() => {
});
},
},
};
</script>
73 changes: 73 additions & 0 deletions src/extensions/iframe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Node } from 'tiptap';
import IframeCommandButton from '../components/MenuCommands/IframeCommandButton.vue';

export default class Iframe extends Node {
get name () {
return 'iframe';
}

get schema () {
return {
attrs: {
src: {
default: null,
},
},
group: 'block',
selectable: false,
parseDOM: [{
tag: 'iframe',
getAttrs: dom => ({
src: dom.getAttribute('src'),
}),
}],
toDOM: node => ['iframe', {
src: node.attrs.src,
frameborder: 0,
allowfullscreen: 'true',
}],
};
}

commands ({ type }) {
return attrs => (state, dispatch) => {
const { selection } = state;
const position = selection.$cursor ? selection.$cursor.pos : selection.$to.pos;
const node = type.create(attrs);
const transaction = state.tr.insert(position, node);
dispatch(transaction);
};
}

get view () {
return {
props: ['node', 'updateAttrs', 'view'],
computed: {
src: {
get () {
return this.node.attrs.src;
},
set (src) {
this.updateAttrs({
src,
});
},
},
},
template: `
<div class="iframe">
<iframe class="iframe__embed" :src="src"></iframe>
</div>
`,
};
}

menuBtnView (editorContext) {
return {
component: IframeCommandButton,
componentProps: {
editorContext,
},
};
}
}
1 change: 1 addition & 0 deletions src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export { default as Image } from './image';
export { default as TodoItem } from './todo_item';
export { default as TodoList } from './todo_list';
export { default as Table } from './table';
export { default as Iframe } from './iframe';

// marks
export { default as Bold } from './bold';
Expand Down

0 comments on commit 290ac2c

Please sign in to comment.