Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
wax-prosemirror
Manage
Activity
Members
Labels
Plan
Issues
34
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wax
wax-prosemirror
Commits
aa74e364
Commit
aa74e364
authored
2 years ago
by
chris
Browse files
Options
Downloads
Patches
Plain Diff
populate headings file
parent
5a2ceb58
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!407
Hhmi fixes
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
wax-prosemirror-services/src/OENContainersService/PopulateHeadingsComponent.js
+93
-0
93 additions, 0 deletions
...ces/src/OENContainersService/PopulateHeadingsComponent.js
with
93 additions
and
0 deletions
wax-prosemirror-services/src/OENContainersService/PopulateHeadingsComponent.js
0 → 100644
+
93
−
0
View file @
aa74e364
/* eslint-disable react/prop-types */
import
React
,
{
useContext
,
useLayoutEffect
,
useEffect
}
from
'
react
'
;
import
{
WaxContext
}
from
'
wax-prosemirror-core
'
;
import
{
DOMParser
}
from
'
prosemirror-model
'
;
import
{
ReplaceStep
,
ReplaceAroundStep
}
from
'
prosemirror-transform
'
;
import
{
Selection
}
from
'
prosemirror-state
'
;
import
{
v4
as
uuidv4
}
from
'
uuid
'
;
import
styled
from
'
styled-components
'
;
import
{
Icon
}
from
'
wax-prosemirror-components
'
;
const
IconRemove
=
styled
(
Icon
)
`
cursor: pointer;
position: relative;
top: 2px;
left: 6px;
height: 16px;
width: 16px;
`
;
const
elementFromString
=
string
=>
{
const
wrappedValue
=
`<body>
${
string
}
</body>`
;
return
new
window
.
DOMParser
().
parseFromString
(
wrappedValue
,
'
text/html
'
).
body
;
};
const
selectionToInsertionEnd
=
(
tr
,
startLen
,
bias
)
=>
{
const
last
=
tr
.
steps
.
length
-
1
;
if
(
last
<
startLen
)
{
return
;
}
const
step
=
tr
.
steps
[
last
];
if
(
!
(
step
instanceof
ReplaceStep
||
step
instanceof
ReplaceAroundStep
))
{
return
;
}
const
map
=
tr
.
mapping
.
maps
[
last
];
let
end
=
0
;
map
.
forEach
((
_from
,
_to
,
_newFrom
,
newTo
)
=>
{
if
(
end
===
0
)
{
end
=
newTo
;
}
});
tr
.
setSelection
(
Selection
.
near
(
tr
.
doc
.
resolve
(
end
),
bias
));
};
export
default
({
setPosition
,
position
})
=>
{
const
context
=
useContext
(
WaxContext
);
const
{
activeView
,
pmViews
:
{
main
},
}
=
context
;
const
isEditable
=
main
.
props
.
editable
(
editable
=>
{
return
editable
;
});
const
readOnly
=
!
isEditable
;
useLayoutEffect
(()
=>
{
const
outlineSection
=
document
.
getElementsByClassName
(
'
outline
'
)[
0
]
.
getBoundingClientRect
();
const
WaxSurface
=
activeView
.
dom
.
getBoundingClientRect
();
const
left
=
outlineSection
.
width
;
const
top
=
outlineSection
.
top
-
WaxSurface
.
top
+
18
;
setPosition
({
...
position
,
left
,
top
});
},
[
position
.
left
]);
useEffect
(()
=>
{},
[]);
const
generateHeadings
=
()
=>
{
const
{
tr
,
selection
:
{
from
,
to
},
}
=
main
.
state
;
const
parser
=
DOMParser
.
fromSchema
(
main
.
state
.
config
.
schema
);
const
content
=
'
<h1>heading when i click note</h1><p>some text when i click note</p>
'
;
const
parsedContent
=
parser
.
parse
(
elementFromString
(
content
));
tr
.
replaceWith
(
from
-
1
,
to
-
1
,
parsedContent
);
selectionToInsertionEnd
(
tr
,
tr
.
steps
.
length
-
1
,
-
1
);
main
.
dispatch
(
tr
);
};
if
(
!
readOnly
)
{
return
<
button
onClick
=
{
generateHeadings
}
>
generate
<
/button>
;
}
};
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment