Skip to content

Commit

Permalink
Merge pull request #10 from masuP9/fix/can-edit-client-side-routing
Browse files Browse the repository at this point in the history
Fix/can edit client side routing
  • Loading branch information
masuP9 authored May 15, 2020
2 parents bb1d508 + 859ed56 commit 62c11c7
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 17 deletions.
6 changes: 2 additions & 4 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
"content_scripts": [
{
"matches": [
"https://note.mu/notes/new",
"https://note.mu/notes/*/edit",
"https://note.com/notes/new",
"https://note.com/notes/*/edit"
"https://note.mu/*",
"https://note.com/*"
],
"js": [
"content_script.js"
Expand Down
12 changes: 11 additions & 1 deletion src/scripts/actions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export const OBSERVE_EDITOR = 'OBSERVE_EDITOR';
export type ObserveEditorAction = {
type: typeof OBSERVE_EDITOR;
payload: Boolean;
};

export function observeEditorAction(isObserve: Boolean): ObserveEditorAction {
return { type: OBSERVE_EDITOR, payload: isObserve };
}

export const SELECT_IMAGE = 'SELECT_IMAGE';
export type SelectImageAction = {
type: typeof SELECT_IMAGE;
Expand Down Expand Up @@ -26,4 +36,4 @@ export function changeSizeImageAction(): ChangeSizeImageAction {
return { type: CHANGE_SIZE_IMAGE };
}

export type ActionTypes = SelectImageAction | DeselectImageAction | ChangeSizeImageAction;
export type ActionTypes = SelectImageAction | DeselectImageAction | ChangeSizeImageAction | ObserveEditorAction;
2 changes: 1 addition & 1 deletion src/scripts/components/AltEditor/AltEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import styled from 'styled-components';
import { Position } from '../../reducer';
import { Position } from '../../reducer/imageReducer';

const Form = styled.form<{ position: Position }>`
position: absolute !important;
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/components/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { AltEditor } from '../AltEditor';
import { Position } from '../../reducer';
import { Position } from '../../reducer/imageReducer';
import { deselectImageAction } from '../../actions';
import { store } from '../../store';

Expand Down
11 changes: 8 additions & 3 deletions src/scripts/observers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { store } from './store';
import { selectImageAction, deselectImageAction, changeSizeImageAction } from './actions';
import { selectImageAction, deselectImageAction, changeSizeImageAction, observeEditorAction } from './actions';

const imageObserver = new MutationObserver((records) => {
records.forEach((record) => {
Expand Down Expand Up @@ -51,7 +51,9 @@ const noteBodyObserver = new MutationObserver((records) => {
export const editorObserver = new MutationObserver((recodes, observer) => {
recodes.forEach((_recode) => {
const noteBody = document.getElementById('note-body');
if (noteBody != undefined) {
const state = store.getState();

if (noteBody != undefined && state.observer.observingEditor !== true) {
const existingImages = noteBody.querySelectorAll('img');
if (existingImages.length > 0) {
existingImages.forEach((img) => {
Expand All @@ -60,7 +62,10 @@ export const editorObserver = new MutationObserver((recodes, observer) => {
}

noteBodyObserver.observe(noteBody, { childList: true });
observer.disconnect();
store.dispatch(observeEditorAction(true));
} else if (noteBody == null && state.observer.observingEditor === true) {
noteBodyObserver.disconnect();
store.dispatch(observeEditorAction(false));
}
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { combineReducers } from 'redux';
import { DESELECT_IMAGE, SELECT_IMAGE, ActionTypes, CHANGE_SIZE_IMAGE } from './actions';
import { DESELECT_IMAGE, SELECT_IMAGE, ActionTypes, CHANGE_SIZE_IMAGE } from '../actions';

export type Position = {
left: number;
Expand Down Expand Up @@ -51,8 +50,3 @@ export function imageReducer(state = initialState, action: Action) {
return state;
}
}

export const rootReducer = combineReducers({
image: imageReducer,
});
export type AppState = ReturnType<typeof rootReducer>;
9 changes: 9 additions & 0 deletions src/scripts/reducer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { combineReducers } from 'redux';
import { imageReducer } from './imageReducer';
import { observerReducer } from './observerReducer';

export const rootReducer = combineReducers({
image: imageReducer,
observer: observerReducer,
});
export type AppState = ReturnType<typeof rootReducer>;
24 changes: 24 additions & 0 deletions src/scripts/reducer/observerReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { OBSERVE_EDITOR, ActionTypes } from '../actions';

export type State = {
observingEditor: Boolean;
};

const initialState: State = {
observingEditor: false,
};

type Action = ActionTypes;

export function observerReducer(state = initialState, action: Action) {
switch (action.type) {
case OBSERVE_EDITOR: {
return {
...state,
observingEditor: action.payload,
};
}
default:
return state;
}
}

0 comments on commit 62c11c7

Please sign in to comment.